Python Pandas 用顶行替换标题

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

Python Pandas Replacing Header with Top Row

pythonpandasheaderrow

提问by Jeremy G

I currently have a dataframe that looks like this:

我目前有一个如下所示的数据框:

           Unnamed: 1    Unnamed: 2   Unnamed: 3  Unnamed: 4
0   Sample Number  Group Number  Sample Name  Group Name
1             1.0           1.0          s_1         g_1
2             2.0           1.0          s_2         g_1
3             3.0           1.0          s_3         g_1
4             4.0           2.0          s_4         g_2

I'm looking for a way to delete the header row and make the first row the new header row, so the new dataframe would look like this:

我正在寻找一种删除标题行并使第一行成为新标题行的方法,因此新数据框将如下所示:

    Sample Number  Group Number  Sample Name  Group Name
0             1.0           1.0          s_1         g_1
1             2.0           1.0          s_2         g_1
2             3.0           1.0          s_3         g_1
3             4.0           2.0          s_4         g_2

I've tried stuff along the lines of if 'Unnamed' in df.columns:then make the dataframe without the header df.to_csv(newformat,header=False,index=False)but I don't seem to be getting anywhere.

我已经尝试了一些方法,if 'Unnamed' in df.columns:然后在没有标题的情况下制作数据框,df.to_csv(newformat,header=False,index=False)但我似乎没有任何进展。

采纳答案by rgalbo

new_header = df.iloc[0] #grab the first row for the header
df = df[1:] #take the data less the header row
df.columns = new_header #set the header row as the df header

回答by JoeCondron

The dataframe can be changed by just doing

只需执行以下操作即可更改数据框

df.columns = df.iloc[0]
df = df[1:]

Then

然后

df.to_csv(path, index=False) 

Should do the trick.

应该做的伎俩。

回答by ostrokach

If you want a one-liner, you can do:

如果你想要一个单线,你可以这样做:

df.rename(columns=df.iloc[0]).drop(df.index[0])

回答by GoPackGo

@ostrokach answer is best. Most likely you would want to keep that throughout any references to the dataframe, thus would benefit from inplace = True.
df.rename(columns=df.iloc[0], inplace = True) df.drop([0], inplace = True)

@ostrokach 答案是最好的。您很可能希望在对数据框的任何引用中都保留它,因此将从 inplace = True 中受益。
df.rename(columns=df.iloc[0], inplace = True) df.drop([0], inplace = True)

回答by rra

--another way to do this

--另一种方式来做到这一点


df.columns = df.iloc[0]
df = df.reindex(df.index.drop(0)).reset_index(drop=True)
df.columns.name = None

    Sample Number  Group Number  Sample Name  Group Name
0             1.0           1.0          s_1         g_1
1             2.0           1.0          s_2         g_1
2             3.0           1.0          s_3         g_1
3             4.0           2.0          s_4         g_2

If you like it hit up arrow. Thanks

如果你喜欢它击中箭头。谢谢

回答by Alex P. Miller

Here's a simple trick that defines column indices "in place". Because set_indexsets rowindices in place, we can do the same thing for columns by transposing the data frame, setting the index, and transposing it back:

这是一个简单的技巧,可以“就地”定义列索引。因为set_index设置了索引,我们可以通过转置数据框、设置索引并将其转回来对列执行相同的操作:

df = df.T.set_index(0).T

Note you may have to change the 0in set_index(0)if your rows have a different index already.

请注意0set_index(0)如果您的行已经有不同的索引,您可能需要更改in 。

回答by Fazley Rafy

header = table_df.iloc[0]
table_df.drop([0], axis =0, inplace=True)
table_df.reset_index(drop=True)
table_df.columns = header
table_df