0 votes
1 view
in Data Science by (15.2k points)

The data I have to work with is a bit messy.. It has header names inside of its data. How can I choose a row from an existing pandas dataframe and make it (rename it to) a column header?

I want to do something like:

header = df[df['old_header_name1'] == 'new_header_name1']

df.columns = header

1 Answer

0 votes
by (23.6k points)

Let us do it with the help of an example:

df = pd.DataFrame([(1,2,3), ('cricket','football','hockey'), (4,5,6)])

 df

Out: 

        0            1           2

0     1           2           3

1     cricket    football   hockey

2     4          5           6

1. Set the column labels to equal the values in the index loc 1:

 df.columns = df.iloc[1]

2. Drop the 2nd row if the index is having unique labels:

 df.drop(df.index[1])

Out: 

1   cricket     football     hockey

0     1          2         3

2     4          5         6

3. Use the below line of code if the index is not unique:

 df.iloc[pd.RangeIndex(len(df)).drop(1)]

Out: 

1   cricket    football   hockey

0     1          2         3

2     4          5         6

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...