import pandas as pd
# I just allowed myself to write 'Person2' instead of 'Person1' at the second row
# of the DataFrame, as I imagine this is what was originally intended in the data,
# but this does not change the method
df = pd.DataFrame({
'Name': ['Person1', 'Person2'],
'SetCode1': ['L6A', 'L6A'],
'SetDetail1': ['B', 'C'],
'SetCode6': ['U2H', None],
'SetDetail6': ['B', None],
})
print(df)
Name SetCode1 SetDetail1 SetCode6 SetDetail6
0 Person1 L6A B U2H B
1 Person2 L6A C None None
# You will need to use reset_index to keep the original index moving forward only if
# the 'Name' column does not have unique values
df_melt = pd.wide_to_long(df, ['SetCode', 'SetDetail'], ['Name'], 'No')
df_out = df_melt[df_melt['SetCode'].notnull()]\
.set_index('SetCode', append=True)\
.reset_index(level=1, drop=True)['SetDetail']\
.unstack()
print(df_out)
SetCode L6A U2H
Name
Person1 B B
Person2 C NaN
使用
pandas.wide_to_long
是正确的解决方案,尽管必须谨慎使用某些列中的NaN
值因此,下面是对Scott Boston答案的改编:
尝试使用
pd.wide_to_long
和unstack
:输出:
我认为这更像是一个列重命名,而不是旋转。这是我的密码
产生
感谢@Sander van den Oord在数据框中输入
相关问题 更多 >
编程相关推荐