I'm trying to concatenate Pandas DataFrame columns replacing “NaN” values with comma.
df = pd.DataFrame({'col1' : ["1","2","3","4","5",np.nan],
'col2' : ["p1","p2","p1",np.nan,"p2",np.nan],
'col3' : ["A","B","C","D","E","F"]})
df
col1 col2 col3
0 1 p1 A
1 2 p2 B
2 3 p1 C
3 4 NaN D
4 5 p2 E
5 NaN NaN F
I need a output :-
col1 col2 col3 col4
0 1 p1 A 1, p1, A
1 2 p2 B 2, p2, B
2 3 p1 C 3, p1, C
3 4 NaN D 4, , D
4 5 p2 E 5, p2, E
5 NaN NaN F , , F
Basically I need equal number of commas for each row in col4
.
Thanks for the help in advance
Replace missing values by DataFrame.fillna
, then use join
per rows:
df['col4'] = df.astype(str).fillna('').apply(', '.join, axis=1)
Or add ,
and use sum
for join, last remove last ,
by Series.str.rstrip
:
df['col4'] = df.astype(str).fillna('').add(', ').sum(axis=1).str.rstrip(', ')
Or processing each column separately:
df['col4'] = (df['col1'].astype(str).fillna('') + ', ' +
df['col2'].astype(str).fillna('') + ', ' +
df['col3'].astype(str))
print (df)
col1 col2 col3 col4
0 1 p1 A 1, p1, A
1 2 p2 B 2, p2, B
2 3 p1 C 3, p1, C
3 4 NaN D 4, , D
4 5 p2 E 5, p2, E
5 NaN NaN F , , F
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.