Python 如何通过熊猫中的循环连接多个数据帧?
我需要通过一个循环合并多个数据帧Python 如何通过熊猫中的循环连接多个数据帧?,python,pandas,Python,Pandas,我需要通过一个循环合并多个数据帧 df1: Id Name 101 Rick 102 Nick 103 Jerry df2: Id Class 101 A 102 B 103 C df3: Id Grade 101 1 102 2 103 3 输出: I tried: data_frames= [] for file in os.listdir(folder_path): file.append(data_frames) final_df
df1:
Id Name
101 Rick
102 Nick
103 Jerry
df2:
Id Class
101 A
102 B
103 C
df3:
Id Grade
101 1
102 2
103 3
输出:
I tried:
data_frames= []
for file in os.listdir(folder_path):
file.append(data_frames)
final_df = pd.merge(data_frames, how='left', on='Id')
TypeError: merge() missing 1 required positional argument: 'right'
from functools import reduce
reduce(lambda x,y: x.merge(y,on='Id', how='left'), dataframes )
方法是使用
functools.reduce
:
Id Name Class Grade
101 Rick A 1
102 Nick B 2
103 Jerry C 3
输出:
I tried:
data_frames= []
for file in os.listdir(folder_path):
file.append(data_frames)
final_df = pd.merge(data_frames, how='left', on='Id')
TypeError: merge() missing 1 required positional argument: 'right'
from functools import reduce
reduce(lambda x,y: x.merge(y,on='Id', how='left'), dataframes )
您可以使用
concat
合并索引上的多个数据帧
Id Name Class Grade
0 101 Rick A 1
1 102 Nick B 2
2 103 Jerry C 3