本文介绍了Python Pandas 用顶行替换标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我目前有一个如下所示的数据框:
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
我已经尝试了 if 'Unnamed' in df.columns:
的内容,然后制作没有标题的数据框 df.to_csv(newformat,header=False,index=假)
但我似乎没有得到任何结果.
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.
推荐答案
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
这篇关于Python Pandas 用顶行替换标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!