我是熊猫新手,请原谅我,如果我听起来太天真了。我有两个数据帧df1和df2,
myext
我想在df1上合并df2,如
df1 = pd.DataFrame({'key1': ['K0', 'K1', 'K2', 'K3'],
'key2': ['K5', 'K4', 'K5', 'K4']})
df2 = pd.DataFrame({'key1': ['K0', 'K1', 'K2', 'K3', 'K9', 'K8', 'K7'],
'key2': ['K5', 'K6', 'K5', 'K4', 'K6', 'K4', 'K5'],
'A':['1', '2', '3', '4', '5', '6', '7'],
'B':['8', '9', '10', '11', '12', '13', '14']})
然后将df2中的剩余值作为一个数据帧。
任何帮助将不胜感激。感谢。
答案 0 :(得分:0)
IIUC您需要使用参数TO_REMOVE_EMPTY = %w|password|
params.delete_if { |k, v| TO_REMOVE_EMPTY.include?(k) && v.empty? }
进行外部联接,然后按boolean indexing
进行拆分:
indicator
final = df1.merge(df2, how='outer', indicator=True)
print (final)
key1 key2 A B _merge
0 K0 K5 1 8 both
1 K1 K4 NaN NaN left_only
2 K2 K5 3 10 both
3 K3 K4 4 11 both
4 K1 K6 2 9 right_only
5 K9 K6 5 12 right_only
6 K8 K4 6 13 right_only
7 K7 K5 7 14 right_only
print (final[final._merge == 'right_only'])
key1 key2 A B _merge
4 K1 K6 2 9 right_only
5 K9 K6 5 12 right_only
6 K8 K4 6 13 right_only
7 K7 K5 7 14 right_only
print (final[final._merge != 'right_only'])
key1 key2 A B _merge
0 K0 K5 1 8 both
1 K1 K4 NaN NaN left_only
2 K2 K5 3 10 both
3 K3 K4 4 11 both