使单元格inputpandas数据框中列的名称

比方说,我有一个数据框(使用pandas数据分析库),看起来像这样:

Unnamed Column1: 1 'Puppies' 2 6 3 15 4 13 5 12 

我想让数据框看起来像这样:

  'Puppies' 1 6 2 15 3 13 4 12 5 80 

一个人如何转移一切,包括replace列名的条目。 有没有办法做到这一点?

 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 

请注意,如果标题行缺less列名称,则"Unnamed: 1"听起来像名称pd.read_csv分配给该列。 在这种情况下,你可以用skiprows=1或者header=1来解决这个问题,而不是修补上面的结果。 skiprows=1会导致pd.read_csv跳过第一行,从而自动从第二行读取标题(列名)。