使用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)
答案 0 :(得分:1)
在熊猫中,“标题”是列的名称,与数据框中的数据分开存储。根据您的评论,我认为您需要先更改列名称,然后删除行。
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
此代码之后,df将为:
Job Revenue
0 abc123 1000
1 hey098 2000