Python Pandas Replacing Header with Top Row(Python Pandas 用顶行替换标题)
本文介绍了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 用顶行替换标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Python Pandas 用顶行替换标题


基础教程推荐
猜你喜欢
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01