To copy a new column from another dataframe, you can use the 'merge()' function in pandas.

Here's an example:

import pandas as pd

# create dataframe 1
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# create dataframe 2
df2 = pd.DataFrame({'A': [1, 2, 3], 'C': [7, 8, 9]})

# merge dataframes on column 'A'
merged_df = pd.merge(df1, df2, on='A')

# copy column 'C' from merged_df to df1
df1['C'] = merged_df['C']

print(df1)

Output:

   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

In this example, we create two dataframes ('df1' and 'df2') and merge them on column 'A' using the 'merge()' function. This creates a new dataframe called 'merged_df' that contains columns from both 'df1' and 'df2'.

We then copy the values from column 'C' in 'merged_df' to a new column called 'C' in 'df1'.

Note that this approach assumes that the two dataframes have a common column (in this case, 'A') that can be used to merge them together. If your dataframes don't have a common column, you may need to use a different approach to copy the column.

How to Copy a Column from Another DataFrame in Python Pandas

原文地址: https://www.cveoy.top/t/topic/n6qX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录