Python Forum
Copy a column from one dataframe to another dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy a column from one dataframe to another dataframe
#8
It doesn't make any difference how many "values" there are. The dataframe operations shown are working with series (columns). A column containint 20,000 values works exactly as one containing 4.

This part is just so I have two dataframes to work with.
df1 = pd.DataFrame({"Letters": ["A", "B", "C", "D"]})
df2 = pd.DataFrame({"Numbers": [1, 2, 3, 4]})
If you prefer they could each have 1,000,000 values.

I get an error when I do this:
import pandas as pd

df1 = pd.DataFrame({"Letters": ["A", "B", "C", "D"]})
df2 = pd.DataFrame({"Numbers": [1, 2, 3, 4, 5]})

df1["Integers"] = df2["Number"]
print(df1)
Error:
Traceback (most recent call last): File "...\lib\site-packages\pandas\core\indexes\base.py", line 3621, in get_loc return self._engine.get_loc(casted_key) File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Number' The above exception was the direct cause of the following exception: Traceback (most recent call last): File ...test.py", line 6, in <module> df1["Integers"] = df2["Number"] File "...\lib\site-packages\pandas\core\frame.py", line 3505, in __getitem__ indexer = self.columns.get_loc(key) File "...\lib\site-packages\pandas\core\indexes\base.py", line 3623, in get_loc raise KeyError(key) from err KeyError: 'Number'
That looks a lot like your error. I think (am quite sure) there is no "machine_status" column in df1.
Reply


Messages In This Thread
RE: Copy a column from one dataframe to another dataframe - by deanhystad - Jul-07-2022, 06:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Most efficient way to roll through a pandas dataframe? sawtooth500 1 130 Jun-07-2024, 03:37 AM
Last Post: sawtooth500
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries sawtooth500 14 1,434 Apr-24-2024, 01:42 AM
Last Post: sawtooth500
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 407 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 655 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Dataframe copy warning sawtooth500 4 519 Mar-25-2024, 11:38 PM
Last Post: sawtooth500
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries i sawtooth500 3 2,725 Mar-22-2024, 03:08 AM
Last Post: deanhystad
  Adding PD DataFrame column bsben 2 437 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 845 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Comparing Dataframe to String? RockBlok 2 522 Nov-24-2023, 04:55 PM
Last Post: RockBlok
  Filter data into new dataframe as main dataframe is being populated cubangt 8 1,221 Oct-23-2023, 12:43 AM
Last Post: cubangt

Forum Jump:

User Panel Messages

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