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)