question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataFrame.add doesn't respect column and row indices

See original GitHub issue

If I use DataFrame.add to add two dataframes together, the order of the rows and columns seems to be used to combine the values, rather than the column and row indices.

E.g. in the below, I think I would expect to see 4 + 20 = 24 for the Col1, r2 sum. Instead the output DataFrame does not have indices, and the sums go by column and row order.

const dfd = require("danfojs-node")     // version 0.2.0

let data = {"Col1": [1, 4, 5, 0], 
            "Col2": [2, 0, 1, 4]}
            
let data2 = {"Col2": [1, 5, 20, 10],    //note swapped column names
             "Col1": [20, 2, 1, 2]}

let df = new dfd.DataFrame(data,{index: ["r1", "r2", "r3", "r4"]})
let df2 = new dfd.DataFrame(data2,{index: [ "r2", "r3", "r4","r5"]})   // note offset index

df.print()
df2.print()

let df_new = df.add(df2)

df_new.print()
╔════╀═══════════════════╀═══════════════════╗
β•‘    β”‚ Col1              β”‚ Col2              β•‘
β•Ÿβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ r1 β”‚ 1                 β”‚ 2                 β•‘
β•Ÿβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ r2 β”‚ 4                 β”‚ 0                 β•‘
β•Ÿβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ r3 β”‚ 5                 β”‚ 1                 β•‘
β•Ÿβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ r4 β”‚ 0                 β”‚ 4                 β•‘
β•šβ•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

╔════╀═══════════════════╀═══════════════════╗
β•‘    β”‚ Col2              β”‚ Col1              β•‘
β•Ÿβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ r2 β”‚ 1                 β”‚ 20                β•‘
β•Ÿβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ r3 β”‚ 5                 β”‚ 2                 β•‘
β•Ÿβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ r4 β”‚ 20                β”‚ 1                 β•‘
β•Ÿβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ r5 β”‚ 10                β”‚ 2                 β•‘
β•šβ•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

╔═══╀═══════════════════╀═══════════════════╗
β•‘   β”‚ Col1              β”‚ Col2              β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ 0 β”‚ 2                 β”‚ 22                β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ 1 β”‚ 9                 β”‚ 2                 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ 2 β”‚ 25                β”‚ 2                 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ 3 β”‚ 10                β”‚ 6                 β•‘
β•šβ•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
risenWcommented, Jan 30, 2021

I am new to Danfo (and Pandas for that matter) so not sure if this is related but I have the same when adding JSON.

myjson = [
  { "A": 1, "B": 10 },
  { "A": 2, "B": 20 },
  { "B": 30, "A": 3 },
  { "A": 4, "B": 40 }
]
df = new dfd.DataFrame(myjson)
df.print()
╔═══╀═══════════════════╀═══════════════════╗
β•‘   β”‚ A                 β”‚ B                 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ 0 β”‚ 1                 β”‚ 10                β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ 1 β”‚ 2                 β”‚ 20                β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ 2 β”‚ 30                β”‚ 3                 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ 3 β”‚ 4                 β”‚ 40                β•‘
β•šβ•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Note row 2 is swapped. Danfo simply takes the order of the column, not knowing it’s reading JSON where the position doesn’t matter.

Thanks for sharing this, I do think its a separate issue though.

0reactions
github-actions[bot]commented, Jul 22, 2021

Stale issue message

Read more comments on GitHub >

github_iconTop Results From Across the Web

DataFrame.add doesn't respect column and row indices #101
If I use DataFrame.add to add two dataframes together, the order of the rows and columns seems to be used to combine the...
Read more >
Pandas: Appending a row to a dataframe and specify its index ...
The name of the Series becomes the index of the row in the DataFrame: In [99]: df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D']) In [100]:Β ......
Read more >
Pandas Append – pd.DataFrame.append() | Data Independent
Pandas DataFrame.append() will append rows (add rows) of other DataFrame, Series, Dictionary or list of these to another DataFrame.
Read more >
Merge, join, concatenate and compare - Pandas
You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform...
Read more >
How to add header row to a Pandas Dataframe?
There are basically two approaches to add a header row in Python in case the original data frame doesn't have a header.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found