Python Forum
Pandas copying wrong values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Pandas copying wrong values (/thread-23565.html)



Pandas copying wrong values - vmarg - Jan-05-2020

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



RE: Pandas copying wrong values - sandeep_ganga - Jan-06-2020

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


RE: Pandas copying wrong values - vmarg - Jan-06-2020

Ok, thanks for the explenation.