如何在 Pandas 中的 transpose() 之后删除多余的行(或列)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38148877/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:30:22  来源:igfitidea点击:

How to remove the extra row (or column) after transpose() in Pandas

pythoncsvpandastranspose

提问by Helena K

After using transpose on a dataframe there is always an extra row as a remainder from the initial dataframe's index for example:

在数据帧上使用转置后,总会有一个额外的行作为初始数据帧索引的剩余部分,例如:

import pandas as pd

df = pd.DataFrame({'fruit':['apple','banana'],'number':[3,5]})
df
    fruit  number
0   apple       3
1  banana       5
df.transpose()
        0       1
fruit   apple  banana
number      3       5

Even when i have no index:

即使我没有索引:

df.reset_index(drop = True, inplace = True)
df
    fruit  number
0   apple       3
1  banana       5

df.transpose()
        0       1
fruit   apple  banana
number      3       5

The problem is that when I save the dataframe to a csv file by:

问题是,当我通过以下方式将数据帧保存到 csv 文件时:

df.to_csv(f)

this extra row stays at the top and I have to remove it manually every time.

这个额外的行保持在顶部,我每次都必须手动删除它。

Also this doesn't work:

这也不起作用:

 df.to_csv(f, index = None)

because the old index is no longer considered an index (just another row...).

因为旧索引不再被视为索引(只是另一行......)。

It also happened when I transposed the other way around and I got an extra column which i could not remove.

当我换位时也发生了这种情况,我得到了一个无法删除的额外列。

Any tips?

有小费吗?

回答by user1742571

I had the same problem, I solved it by reseting index before doing the transpose. I mean df.set_index('fruit').transpose():

我遇到了同样的问题,我通过在执行transpose. 我的意思是df.set_index('fruit').transpose()

import pandas as pd

df = pd.DataFrame({'fruit':['apple','banana'],'number':[3,5]})
df
    fruit   number
0   apple   3
1   banana  5

And df.set_index('fruit').transpose()gives:

df.set_index('fruit').transpose()给出:

fruit   apple   banana
number  3       5

回答by Radhika Nair

Instead of removing the extra index, why don't try setting the new index that you want and then use slicing ?

与其删除额外的索引,不如尝试设置您想要的新索引,然后使用切片?

step 1: Set the new index you want:
df.columns = df.iloc[0]
step 2: Create a new dataframe removing extra row.
df_new = df[1:]

第 1 步:设置您想要的新索引:
df.columns = df.iloc[0]
第 2 步:创建一个新的数据框,删除多余的行。
df_new = df[1:]