Alex Dana
Alex Dana

Reputation: 1297

First row to header with pandas

I have the following pandas dataframe df :

import pandas as pd
from io import StringIO
s = '''\
"Unnamed: 0","Unnamed: 1"   
Objet,"Unités vendues"  
Chaise,3
Table,2
Tabouret,1
'''
df = pd.read_csv(StringIO(s))

which looks as:

  Unnamed: 0     Unnamed: 1
0      Objet  Unités vendues
1     Chaise                 3
2      Table                 2
3   Tabouret                 1

My target is to make the first row as header.

I use :

headers = df.iloc[0]
df.columns = [headers]  

However, the "0" appears in index column name (which is normal, because this 0 was in the first row).

0          Objet Unités vendues 
1         Chaise              3 
2          Table              2 

I tried to delete it in many way, but nothing work :

Neither del df.index.name from this post

Neither df.columns.name = None from this post or this one (which is the same situation)

How can I have this expected output :

           Objet Unités vendues 
1         Chaise              3 
2          Table              2 

Upvotes: 7

Views: 40915

Answers (4)

brweb
brweb

Reputation: 61

What worked for me.

Replace:

    headers = df.iloc[0]
    df.columns = [headers]

with:

    headers = df.iloc[0].values
    df.columns = headers
    df.drop(index=0, axis=0, inplace=True)

Using .values returns the values from the row Series as a list which does not include the index value. Reassigning the column headers then works as expected, without the 0. Row 0 still exists so it should be removed with df.drop.

Upvotes: 6

mikecbos
mikecbos

Reputation: 47

Using the skiprows parameter did the job for me: i.e. skiprows=N where N = the number of rows to skip (in the above example, 1), so:

df = pd.read_csv('filename', skiprows=1)

Upvotes: 1

Julian
Julian

Reputation: 152

Having my data in U and my column names in Un I came up with this algorithm. If you can shorten it, please do so.

U = pd.read_csv('U.csv', header = None) #.to_numpy()
Un = pd.read_csv('namesU.csv', header=None).T # Read your names csv, in my case they are in one column
Un = Un.append(U) # append the data U to the names
Un.reset_index(inplace=True, drop=True) # reset the index and drop the old one, so you don't have duplicated indices
Un.columns = [Un.iloc[0]] # take the names from the first row
Un.drop(index=0, inplace=True) # drop the first row
Un.reset_index(inplace=True, drop=True) # Return the index counter to start from 0

Another option:

Un = pd.read_csv('namesY.csv', header=None) # Read your names csv, in my case they are in one column
Un = list( Un[0] ) 
Un = pd.DataFrame(U, columns=[Un])

Upvotes: 1

Paulo Caldas
Paulo Caldas

Reputation: 93

what about defining that when you load your table in the first place?

pd.read_csv('filename', header = 1)

otherwise I guess you can just do this:

df.drop('0', axis = 1)

Upvotes: 3

Related Questions