选项0
超级简单
pd.concat([pd.Series(df[c].dropna().values, name=c) for c in df], axis=1) col1 col2 col3 col4 col5 col60 ABC1 15.0 24RA Large 345.0 US
每列可以处理多个值吗?
我们当然可以!
df.loc[2, 'col3'] = 'Test' col1 col2 col3 col4 col5 col60 ABC1 15.0 Test Large 345.0 US1 NaN NaN 24RA NaN NaN NaN
选项1
使用np.where
像外科医生一样的通用解决方案
v = df.valuesi, j = np.where(np.isnan(v))s = pd.Series(v[i, j], df.columns[j])c = s.groupby(level=0).cumcount()s.index = [c, s.index]s.unstack(fill_value='-') # <-- don't fill to get NaN col1 col2 col3 col4 col5 col60 ABC1 15.0 24RA Large 345 US
df.loc[2, 'col3'] = 'Test'v = df.valuesi, j = np.where(np.isnan(v))s = pd.Series(v[i, j], df.columns[j])c = s.groupby(level=0).cumcount()s.index = [c, s.index]s.unstack(fill_value='-') # <-- don't fill to get NaN col1 col2 col3 col4 col5 col60 ABC1 15.0 Test Large 345 US1 - - 24RA - - -
选项2mask
制作null然后stack
摆脱它们
否则我们可以
# This should work even if `'-'` are NaN# but you can skip the `.mask(df == '-')`s = df.mask(df == '-').stack().reset_index(0, drop=True)c = s.groupby(level=0).cumcount()s.index = [c, s.index]s.unstack(fill_value='-') col1 col2 col3 col4 col5 col60 ABC1 15.0 Test Large 345 US1 - - 24RA - - -



