Pandas - header doesn't change after dropping rows

Viewed 412

After dropping a few rows of a DataFrame using Pandas, the header doesn't change; it keeps the way it was before the rows were dropped.

How can I get the updated header?

for row in range(rowStart): # rowStart is my index (int). It means it should drop all rows up to this
    df.drop(row, inplace=True)
df = df.reset_index(drop=True)

header = list(df) # even assigning the header after the drop, it keeps returning the same as before
print(header)
print('')
print(df) # the DataFrame is ok, without the removed rows (as expected)

Minimal example:

data = {
    '': '',
    'asd': '',
    'bfdgfd': '',
    'trytr': '',
    'jlhj': '',
    'Job': 'Revenue',
    'abc123': 1000.00,
    'hey098': 2000.00
}
df = pd.DataFrame(data.items(),
    columns=['Unnamed: 0', 'Unnamed: 1'])
header = list(df)
print(header)
print('')
print(df)

startRow = 5

for row in range(startRow):
    df.drop(row, inplace=True)
df = df.reset_index(drop=True)
header = list(df)
print(header)
print('')
print(df)
1 Answers

In pandas, the "header" is the name of the columns and is stored separately from the data in the dataframe. Based on your comments, I think you need to change the column names first and then drop the rows.

import pandas as pd

data = {
    '': '',
    'asd': '',
    'bfdgfd': '',
    'trytr': '',
    'jlhj': '',
    'Job': 'Revenue',
    'abc123': 1000.00,
    'hey098': 2000.00
}
df = pd.DataFrame(data.items(),
    columns=['Unnamed: 0', 'Unnamed: 1'])
startRow = 5

df.columns = df.loc[startRow].to_list()  # set the "header" to the values in this row
df = df.loc[startRow+1:].reset_index(drop=True)  # select only the rows you want

After this code, df will be:

      Job Revenue
0  abc123    1000
1  hey098    2000
Related