pd.read_csv()的数据:
Name Job Place Age John None None None None Doctor None None None None UK None None None None 50 Alex None None None None Engr None None None None US None None None None 45
@H_404_7@单行的信息包含在对角线中.有没有办法将对角线转换并折叠成行?结果数据帧将有2行.
尝试使用df.ffill()/ df.bfill()和df.drop_duplicates(),但这不起作用.
解决方法:
您可以使用:
#change string None to NaN df = df.replace({'None':np.nan}) #multiindex df.index = [df.index, df.Name.notnull().cumsum() - 1] #remove nan by stack df = df.stack().reset_index(name='val') #pivoting df = df.pivot(index='Name', columns='level_2', values='val') print (df) level_2 Age Job Name Place Name 0 50 Doctor John UK 1 45 Engr Alex US
@H_404_7@版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。