Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 合并后保留数据帧的顺序_Python_Python 3.x_Pandas_Dataframe_Merge - Fatal编程技术网

Python 合并后保留数据帧的顺序

Python 合并后保留数据帧的顺序,python,python-3.x,pandas,dataframe,merge,Python,Python 3.x,Pandas,Dataframe,Merge,我有一个数据帧df1,它需要添加一个新列'h[mm]。新的'h[mm]中的值应根据df1的'Profile'列中的值在df2中查找。这种情况可以与Excel的“vlookup”或“索引/匹配”相比较 我已成功地将具有正确值的新列'h[mm]添加到df1。但是,它更改了原始df1的顺序,这在我的情况下是不需要的 数据帧df1和df2的简化可复制版本为: pile_type = ['P01', 'P20', 'P05', 'P23', 'P04', 'P01'] profile = ['HE200A

我有一个数据帧
df1
,它需要添加一个新列
'h[mm]
。新的
'h[mm]
中的值应根据
df1
'Profile'
列中的值在
df2
中查找。这种情况可以与Excel的“vlookup”或“索引/匹配”相比较

我已成功地将具有正确值的新列
'h[mm]
添加到
df1
。但是,它更改了原始
df1
的顺序,这在我的情况下是不需要的

数据帧
df1
df2
的简化可复制版本为:

pile_type = ['P01', 'P20', 'P05', 'P23', 'P04', 'P01']
profile = ['HE200A', 'HE220A', 'HE240B', 'NaN', 'HE200A', 'HE300B']
df1 = pd.DataFrame({'Pile_type': pile_type, 'Profile': profile})

profile_database = ['HE200A', 'HE220A', 'HE240B', 'HE500B', 'HE300B']
profile_height_database = [190, 210, 240, 500, 300]
df2 = pd.DataFrame({'Profile': profile_database, 'h[mm]': profile_height_database})
我找到的最接近解决方案是使用
pd.merge\u ordered()

在生成的
df_mo
中,仍不保留
df1
的原始顺序:

print('df1:', df1)
print('df2:', df2)
print('df_mo:', df_mo)

df1:    Pile_type Profile
0       P01       HE200A
1       P20       HE220A
2       P05       HE240B
3       P23       NaN
4       P04       HE200A
5       P01       HE300B

df2:  Profile h[mm]
0     HE200A  190
1     HE220A  210
2     HE240B  240
3     HE500B  500
4     HE300B  300

df_mo:  Pile_type Profile  h[mm]
0       P01       HE200A   190.0
1       P04       HE200A   190.0
2       P20       HE220A   210.0
3       P05       HE240B   240.0
4       P23       NaN      NaN
5       P01       HE300B   300.0
我还尝试了标准的
pd.merge()
,但运气不佳。

使用:
df1.merge(df2,how='left')

正如@Graipher指出的,
how
参数是控制结果合并的
DataFrame

方式:{'left','right','outer','inner'},默认为'inner'

  • 左:仅使用左框架中的键,类似于SQL左外部联接; 保留密钥顺序


how='left'
部分实际上是它保持秩序的原因
print('df1:', df1)
print('df2:', df2)
print('df_mo:', df_mo)

df1:    Pile_type Profile
0       P01       HE200A
1       P20       HE220A
2       P05       HE240B
3       P23       NaN
4       P04       HE200A
5       P01       HE300B

df2:  Profile h[mm]
0     HE200A  190
1     HE220A  210
2     HE240B  240
3     HE500B  500
4     HE300B  300

df_mo:  Pile_type Profile  h[mm]
0       P01       HE200A   190.0
1       P04       HE200A   190.0
2       P20       HE220A   210.0
3       P05       HE240B   240.0
4       P23       NaN      NaN
5       P01       HE300B   300.0
df1.merge(df2, on='Profile', how='left')

  Pile_type Profile  h[mm]
0       P01  HE200A  190.0
1       P20  HE220A  210.0
2       P05  HE240B  240.0
3       P23     NaN    NaN
4       P04  HE200A  190.0
5       P01  HE300B  300.0