Python 使单元格条目成为数据框中列的名称

Python 使单元格条目成为数据框中列的名称,python,excel,numpy,pandas,scipy,Python,Excel,Numpy,Pandas,Scipy,假设我有一个数据框(使用pandas数据分析库),看起来是这样的: Unnamed Column1: 1 'Puppies' 2 6 3 15 4 13 5 12 我想让数据帧看起来像这样: 'Puppies' 1 6 2 15 3 13 4 12 5 80 如何将所有内容向上移动,包括替换列名的条目。有办法做到这一点吗 df.columns = df.iloc[0] # set the columns to the values in the first

假设我有一个数据框(使用pandas数据分析库),看起来是这样的:

   Unnamed Column1:
1  'Puppies'
2  6
3  15
4  13
5  12
我想让数据帧看起来像这样:

   'Puppies'
1  6
2  15
3  13
4  12
5  80
如何将所有内容向上移动,包括替换列名的条目。有办法做到这一点吗

df.columns = df.iloc[0]   # set the columns to the values in the first row
df = df.iloc[1:]          # reassign df to all rows but the first

请注意,
“Unnamed:1”
听起来很像
pd.read\u csv
指定给列的名称,如果标题行缺少列名。在这种情况下,您可以使用或
header=1
来解决问题,而不是像上面所示修补结果
skiprows=1
将导致pd.read\u csv跳过第一行,从而自动从第二行读取标题(列名)



请注意,
“Unnamed:1”
听起来很像
pd.read\u csv
指定给列的名称,如果标题行缺少列名。在这种情况下,您可以使用或
header=1
来解决问题,而不是像上面所示修补结果
skiprows=1
将导致pd.read\u csv跳过第一行,从而自动从第二行读取标题(列名)

谢谢,这很有帮助!谢谢,这很有帮助!