如何在 pandas 数据框中切换列行 [英] how to switch columns rows in a pandas dataframe

查看:54
本文介绍了如何在 pandas 数据框中切换列行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

                                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

我尝试过数据透视表

test.pivot(index=index, columns='0', values='1')

但出现以下错误:

KeyError: '1'

数据透视表有其他替代方法吗?

any alternative to pivot table to do this?

推荐答案

您可以使用df = df.T来转置数据帧.这样可以将数据框切换为圆形,使行成为列.

You can use df = df.T to transpose the dataframe. This switches the dataframe round so that the rows become columns.

您还可以使用 pd.DataFrame.transpose() .

You could also use pd.DataFrame.transpose().

这篇关于如何在 pandas 数据框中切换列行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆