使用 Pandas 删除 DataFrame 的几行后,标题不会改变;它保持行被删除之前的状态。
我怎样才能得到更新的标题?
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)
最小的例子:
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)
ABOUTYOU
相关分类