Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Pandas copying wrong values
#1
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
Quote
#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
Quote
#3
Ok, thanks for the explenation.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas + Groupby + Filter unique values JosepMaria 1 253 Jun-15-2020, 08:15 AM
Last Post: JosepMaria
  Copying from windows Clipboard VincentAdey 3 357 Apr-17-2020, 02:47 PM
Last Post: buran
  sort values of a column pandas karlito 2 689 Oct-22-2019, 06:11 AM
Last Post: karlito
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 606 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  Custom timeinterval converted to hourly values using Pandas? SinPy 1 955 Jun-07-2019, 05:06 AM
Last Post: heiner55
  Splitting values in column in a pandas dataframe based on a condition hey_arnold 1 2,252 Jul-24-2018, 02:18 PM
Last Post: hey_arnold
  Pandas null values rmichalowski 3 8,063 May-15-2017, 07:58 PM
Last Post: zivoni

Forum Jump:


Users browsing this thread: 1 Guest(s)