向下移动列标题和数据值

问题描述 投票:0回答:1

enter image description here

如您所见,列标题从顶部开始,但是我希望它从与名称,年龄等相同的行开始。是否有此代码?我已经尝试过lloc,shift和它不起作用。品牌是我创建的新列,它不在主数据内。

这是我的代码:

df = pd.read_excel(myfile,header=none)
brand = 'nike'
df['Brand']= brand
python pandas
1个回答
0
投票

如果知道要使用的文件中标题行的编号,只需将header=定义传递给标题的行编号即可(通过打开excel即可找到它:]]

说我有文件:

15    16    17    18   19   Brand
a     b     c     d    e    f
Name  Age   Type  Size Comment Brand
1     2     3     4    Hi      Nike

我知道标题在第三行,但是python索引从0开始,所以:

df = pd.read_excel(myexcel, header=2)
print(df)

输出:

   Name  Age  Type  Size Comment Brand
0     1    2     3     4      Hi  Nike
© www.soinside.com 2019 - 2024. All rights reserved.