Reputation: 131
How to merge df1 and df2 into df?
df1 = pd.DataFrame(list(zip(['4.txt', '5.txt', '6.txt'], [1,0,0], [1,1,1])),
columns = ['file', 'is_about_heroes', 'is_about_pony'])
df2 = pd.DataFrame(list(zip(['5.txt', '6.txt'], [1,0], [1,0])),
columns = ['file', 'is_about_pony', 'is_about_wolf'])
df1
file is_about_heroes is_about_pony
0 4.txt 1 1
1 5.txt 0 1
2 6.txt 0 1
df2
file is_about_pony is_about_wolf
0 5.txt 1 1
1 6.txt 0 0
And I want to get df which is the boolean union of two previous dfs.
df = pd.DataFrame(list(zip(['4.txt', '5.txt', '6.txt'], [1,0,0], [1,1,1], [0,1,0])),
columns = ['file', 'is_about_heroes', 'is_about_pony', 'is_about_wolf'])
file is_about_heroes is_about_pony is_about_wolf
0 4.txt 1 1 0
1 5.txt 0 1 1
2 6.txt 0 1 0
Is it possible without several manual cycles?
Upvotes: 1
Views: 196
Reputation: 210852
Try to use merge
:
In [186]: df1.merge(df2, how='left').fillna(0)
Out[186]:
file is_about_heroes is_about_pony is_about_wolf
0 4.txt 1 1 0.0
1 5.txt 0 1 1.0
2 6.txt 0 1 0.0
Upvotes: 1