我有以下框架:
col1 col1
null hi
hello null
我正在尝试合并两列,因为它们具有相同的名称:
col1
hi
hello
我正在使用:
result= result.groupby(result.columns, axis=1).first()
但是第一个()将其应用于整个列,所以我得到:
col1
hi
null
任何人都可以合并列并避免空值。
最佳答案
如果将 null
转换为 NaN
,您的解决方案就可以正常工作。
所以问题可能是 null
有时是 string
,所以首先替换
。
df = df.replace('null', np.nan).groupby(df.columns, axis=1).first()
print (df)
col1
0 hi
1 hello
关于python - pandas 合并同名列分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52948883/