Python 如何在熊猫数据框中切换列行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31658183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to switch columns rows in a pandas dataframe
提问by Blue Moon
I have the following dataframe:
我有以下数据框:
0 1
0 enrichment_site value
1 last_updated value
2 image_names value
3 shipping_weight value
4 ean_gtin value
5 stockqty value
6 height__mm value
7 availability value
8 rrp value
9 sku value
10 price_band value
11 item value
I tried with pivot table
我尝试使用数据透视表
test.pivot(index=index, columns='0', values='1')
but I get the following error:
但我收到以下错误:
KeyError: '1'
any alternative to pivot table to do this?
有什么替代数据透视表来做到这一点?
采纳答案by Jamie Bull
You can use df = df.T
to transpose the dataframe. This switches the dataframe round so that the rows become columns.
您可以使用df = df.T
转置数据帧。这会轮换数据框,以便行变成列。
You could also use pd.DataFrame.transpose()
.
您也可以使用pd.DataFrame.transpose()
.
回答by Jan H.
When using pd.DataFrame.transpose (as suggested by Jamie Bull / coldspeed), be sure to actually write
使用 pd.DataFrame.transpose 时(如 Jamie Bull/coldspeed 所建议的),请务必实际编写
...without the brackets, it didn't work for me.
...没有括号,它对我不起作用。