[英]Convert some dataframe rows into columns in pandas
我尋找其他答案,但我找不到我的問題的答案。
我有這個 dataframe:
Category 3,Chapter I,Category 2,Category 1,Chapter II,Chapter III
prima,126,Immediate corrections,Immediate corrections,97,83
da,38,Small late single corrections,Late corrections,24,24
sps.,22,Late single corrections written above,Late corrections,17,18
我希望這是:
prima, da, sps.,
Chapter I 126, 38, 22
Chapter II 97, 24, 17
Chapter III 83, 24, 18
類別 I 將其他類別分組,我不知道如何處理它們,它們也可以刪除。
我該怎么做?
在將列類別 3 設置為索引后,您是否考慮過對 dataframe 進行轉置?
想象一下 df 是:
a b
0 1 7
1 2 8
2 3 9
然后:
df.set_index("a")
df.T
a 1 2 3
b 7 8 9
最后,您可以使用loc
過濾您感興趣的那些列
df = df.drop([x for x in df.columns if not 'Chapter' in x], axis=1).T
混合以下兩個答案:
df.set_index('Category 3')[[x for x in df.columns if 'Chapter' in x]].T
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.