1
假設我有2個數據幀。我想基於列查找將數據幀1列添加到數據幀2。如果連接不可行,我想在額外的列中保持一定的常量(所以我可以過濾)。熊貓根據連接將列從一個數據框添加到另一個數據框
圖形:
代碼:
import pandas as pd
import numpy as np
data = np.array([['','Col1','Col2'],
['Row1','2','TWO'],
['Row2','1','ONE']]
)
data2 = np.array([['','Col3','Col4'],
['Row1','1','T1'],
['Row2','2','T2'],
['Row3','3','T3']]
)
df = pd.DataFrame(data=data[1:,1:],
index=data[1:,0],
columns=data[0,1:])
df2 = pd.DataFrame(data=data2[1:,1:],
index=data2[1:,0],
columns=data2[0,1:])
result_df = df2 + join Col2 based on df2.Col3 = df.Col1. Add certain string constant if join fails.
print(df)
print(df2)
print(result_df)