Python Forum

Full Version: Pandas copying wrong values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can someone explain me this?
Python 3.7
pandas 0.25.3

import pandas as pd

foo = pd.concat([pd.DataFrame({'foo': [10,20]}), pd.DataFrame({'foo': [30,40]})])
bar = pd.DataFrame({'bar': [1, 2, 3, 4]})

foo["bar"] = bar["bar"]

print((foo["bar"].values == bar["bar"].values).all())
print(foo)
Output:
False foo bar 0 10 1 1 20 2 0 30 1 1 40 2
If we doesn't define ignore_index parameter, the concat/append will add second dataframe rows with the original index numbers it had, thus the new added rows have same index again which start with 0.


import pandas as pd
 
foo = pd.concat([pd.DataFrame({'foo': [10,20]}), pd.DataFrame({'foo': [30,40]})],ignore_index=True)
bar = pd.DataFrame({'bar': [1, 2, 3, 4]})
 
foo["bar"] = bar["bar"]
 
print((foo["bar"].values == bar["bar"].values).all())
print(foo)
Output:
True foo bar 0 10 1 1 20 2 2 30 3 3 40 4
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Ok, thanks for the explenation.