Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k 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 (41.4k 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

If you are interested to learn Pandas visit this Python Pandas Tutorial.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...