用顶行替换标题
- 2025-04-15 09:21:00
- admin 原创
- 30
问题描述:
我目前有一个如下所示的数据框:
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
我正在寻找一种方法来删除标题行并将第一行设为新的标题行,因此新的数据框将如下所示:
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=False)
但我似乎没有取得任何进展。
解决方案 1:
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
解决方案 2:
只需执行以下操作即可更改数据框
df.columns = df.iloc[0]
df = df[1:]
然后
df.to_csv(path, index=False)
应该可以。
解决方案 3:
如果你想要一行代码,你可以这样做:
df.rename(columns=df.iloc[0]).drop(df.index[0])
解决方案 4:
另一个使用 Python 交换的单行代码:
df, df.columns = df[1:] , df.iloc[0]
这不会重置索引
但相反的情况不会像预期的那样df.columns, df = df.iloc[0], df[1:]
解决方案 5:
这里有一个“就地”定义列索引的简单技巧。因为set_index
设置行索引是就地的,所以我们可以通过转置数据框、设置索引,然后再转置回去,对列执行相同的操作:
df = df.T.set_index(0).T
请注意,如果您的行已经有不同的索引,则可能需要进行更改0
。set_index(0)
解决方案 6:
@ostrokach 的答案最好。你很可能希望在对 DataFrame 的所有引用中都保留这一点,因此 inplace = True 会对你有所帮助。
`df.rename(columns=df.iloc[0], inplace = True)
df.drop([0], inplace = True)`
解决方案 7:
或者,我们可以在使用 pandas 读取文件时执行此操作。
在这种情况下我们可以使用,
pd.read_csv('file_path',skiprows=1)
读取文件时,这将跳过第一行并将该列设置为文件的第二行。
解决方案 8:
由于某种原因,我不得不这样做:
df.columns = [*df.iloc[0]]
df = table[1:]
我将列表拆分成列表的部分看起来是多余的,但除此之外,标题仍然作为实际表格的一部分出现。
解决方案 9:
如果你从列表列表开始
pd.DataFrame(input[1:], columns=input[0])
解决方案 10:
——另一种方法
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
如果您喜欢,请点击向上箭头。谢谢
解决方案 11:
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
解决方案 12:
这似乎是一项可能需要多次执行的任务。我参考了 rgalbo 的答案,并编写了一个简单的函数,可以提取并放入任何项目中。
def promote_df_headers(df):
'''
Takes a df and uses the first row as the header
Parameters
----------
df : DataFrame
Any df with one or more columns.
Returns
-------
df : DataFrame
Input df with the first row removed and used as the column names.
'''
new_header = df.iloc[0]
df = df[1:]
df.columns = new_header
df = df.reset_index(drop=True)
return df
解决方案 13:
当使用 Pandas 读取文件时,这对我来说很有用:
pd.read_csv('file_path',header=0)
解决方案 14:
最佳实践和最佳 OneLiner:
df.to_csv(newformat,header=1)
注意标题值:
标题指的是用作列名的行号。别搞错了,行号不是 df 中的,而是来自 excel 文件中的(0 是第一行,1 是第二行,以此类推)。
这样,您将获得所需的列名,而不必编写额外的代码或创建新的 df。
好消息是,它会删除被替换的行。
扫码咨询,免费领取项目管理大礼包!