'Merge Rows in pandas dataframe having same string in a column
I'm new to python. I've 2 dataframes:
df1:
Col1 Col2 Col3 Col4
0 A A1 A2 0
1 B B1 B2 0
2 C C1 Nan 0
df2:
Col1 Col2 Col3 Col4
1 B NaN NaN B3
4 C NaN C2 C3
Expected Out:
Col1 Col2 Col3 Col4
0 A A1 A2 0
1 B B1 B2 B3
2 C C1 C2 C3
tried pd.concat(), it merged both data frames having dublicate strings in columns.
want to merge two rows having same string value in 1st column. Any help will be appreciated, Thank You.
Solution 1:[1]
Use df.set_index with df.update and df.reset_index:
In [1355]: df1 = df1.set_index('Col1')
In [1356]: df1.update(df2.set_index('Col1'))
In [1358]: df1.reset_index(inplace=True)
In [1359]: df1
Out[1359]:
Col1 Col2 Col3 Col4
0 A A1 A2 0
1 B B1 B2 B3
2 C C1 C2 C3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
