我有2个具有相同架构和不同数据的数据框。我想将它们两者进行比较,并获取所有列具有不同值的所有行。
“ df1”:
id Store is_open
1 'Walmart' true
2 'Best Buy' false
3 'Target' true
4 'Home Depot' true
“ df2”:
id Store is_open
1 'Walmart' false
2 'Best Buy' true
3 'Target' true
4 'Home Depot' false
我能够得到区别,但是我没有得到所有列,而只是得到了已更改的列。所以我得到以下输出:
result_df:
id is_open is_open
1 true false
2 false true
4 true false
以下是实现上述输出的代码:
ne_stacked = (from_aoi_df != to_aoi_df).stack()
changed = ne_stacked[ne_stacked]
changed.index.names = ['id', 'col_changed']
difference_locations = np.where(from_aoi_df != to_aoi_df)
changed_from = from_aoi_df.values[difference_locations]
changed_to = to_aoi_df.values[difference_locations]
df5=pd.DataFrame({'from': changed_from, 'to': changed_to})
df5
但是,除了以上结果之外,我还希望所有相同的列也添加了Store列,所以我的预期输出是:
expected_result_df:
id Store is_open_df1 is_open_df2
1 Walmart true false
2 Best Buy false true
4 Home Depot true false
我该如何实现?
答案 0 :(得分:2)
使用熊猫InvalidArgumentError: No OpKernel was registered to support Op 'MaxBytesInUse' with these attrs. Registered devices: [CPU], Registered kernels:
device='GPU'
[[Node: PeakMemoryTracker/MaxBytesInUse = MaxBytesInUse[_device="/device:GPU:0"]()]]
功能
merge
过滤出df = pd.merge(df1,df2[['id','is_open']],on='id')
列不相等的行
is_open
按照您的期望df = df[df["is_open_x"]!=df["is_open_y"]]
df
列
rename
答案 1 :(得分:1)
new_df = pd.concat([df1, df2]).reset_index(drop=True)
df = new_df.drop_duplicates(subset=['col1','col2'], keep=False)
这将为您提供一个名为df的数据框,其中仅包含不同的记录。
df=np.where(df1==df2,'true','false')
希望这会有所帮助!! 如果df1和df2具有唯一值,则可以使用...如果在其中存在重复项,可以删除重复项。
答案 2 :(得分:0)
怎么样?
df1['is_open_df2'] = df2['is_open']
expected_result_df = df1[df1['is_open'] != df1[is_open_df2']]
答案 3 :(得分:0)
使用:
#compare DataFrames
m = (from_aoi_df != to_aoi_df)
#check at least one True per columns
m1 = m.any(axis=0)
#check at least one True per rows
m2 = m.any(axis=1)
#filter only not equal values
df1 = from_aoi_df.loc[m2, m1].add_suffix('_df1')
df2 = to_aoi_df.loc[m2, m1].add_suffix('_df2')
#filter equal values
df3 = from_aoi_df.loc[m2, ~m1]
#join together
df = pd.concat([df3, df1, df2], axis=1)
print (df)
id Store is_open_df1 is_open_df2
0 1 Walmart True False
1 2 Best Buy False True
3 4 Home Depot True False
验证具有多个已更改列的解决方案:
#changed first value id column
print (from_aoi_df)
id Store is_open
0 10 Walmart True
1 2 Best Buy False
2 3 Target True
3 4 Home Depot True
m = (from_aoi_df != to_aoi_df)
m1 = m.any(axis=0)
m2 = m.any(axis=1)
df1 = from_aoi_df.loc[m2, m1].add_suffix('_df1')
df2 = to_aoi_df.loc[m2, m1].add_suffix('_df2')
df3 = from_aoi_df.loc[m2, ~m1]
df = pd.concat([df3, df1, df2], axis=1)
print (df)
Store id_df1 is_open_df1 id_df2 is_open_df2
0 Walmart 10 True 1 False
1 Best Buy 2 False 2 True
3 Home Depot 4 True 4 False