Python Forum
Pandas copying wrong values
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
Reply
#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
Reply
#3
Ok, thanks for the explenation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning conditional values in Pandas Scott 3 725 Dec-19-2023, 03:10 AM
Last Post: Larz60+
Question How does one clean a populated table in MySQL/MariaDB? Copying values across tables? BrandonKastning 2 1,539 Jan-17-2022, 05:46 AM
Last Post: BrandonKastning
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,525 Jan-10-2022, 04:42 PM
Last Post: moduki1
  pandas: Compute the % of the unique values in a column JaneTan 1 1,756 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,094 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  Pandas + Groupby + Filter unique values JosepMaria 1 2,838 Jun-15-2020, 08:15 AM
Last Post: JosepMaria
  sort values of a column pandas karlito 2 2,469 Oct-22-2019, 06:11 AM
Last Post: karlito
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 2,602 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  Custom timeinterval converted to hourly values using Pandas? SinPy 1 2,752 Jun-07-2019, 05:06 AM
Last Post: heiner55
  Splitting values in column in a pandas dataframe based on a condition hey_arnold 1 4,139 Jul-24-2018, 02:18 PM
Last Post: hey_arnold

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020