我有两列键和值(继承了旧的错误设计),我想根据键列将这些列分成多个其他列,并将值用作行中的值。| Key | Value ||-----------------------------|-----------------------------|| . | None || Review Required? | Yes || Installed? | No || Required Publish? | No || Published? | Yes || Published? | No || Consult Required? | Yes || IS Catalog Reference | Null || License Review Required? | No |我尝试过这样的事情,但没有奏效 df.loc[~df['Key'].isnull()] .apply(lambda cols: pd.DataFrame({cols['Key']:cols['Value']},index=[1]),axis=0)结果是我希望的示例:| Review Required? | Installed? | Required Publish? | Published? | Published? ||------------------|-------------|-------------------|--------------|-------------|| Yes | None | None | None | None || None | No | None | None | None || None | None | No | None | None || None | None | None | Yes | None || None | None | None | None | No |这种情况发生了很多次,而不仅仅是是或否。提前致谢。
1 回答
慕斯709654
TA贡献1840条经验 获得超5个赞
用于DataFrame.dropna
删除缺少值的行,添加Key
到索引并通过以下方式重塑Series.unstack
:
df = df.dropna(subset=['Key']).set_index('Key', append=True)['Value'].unstack()
添加回答
举报
0/150
提交
取消