Python 如何在数据帧上循环并删除行?
我试图在一个数据帧上循环并删除“player\u fifa\u api\u id”列中的值与前一行中的值相等的行。出于某种原因,我的代码不起作用:Python 如何在数据帧上循环并删除行?,python,pandas,dataframe,Python,Pandas,Dataframe,我试图在一个数据帧上循环并删除“player\u fifa\u api\u id”列中的值与前一行中的值相等的行。出于某种原因,我的代码不起作用: for i in range(0,len(test)-1): print("{} lines out of {} processed".format(i,len(test))) if test['player_fifa_api_id'].iloc[i+1] == test['player_fifa_api_id'].iloc[i]:
for i in range(0,len(test)-1):
print("{} lines out of {} processed".format(i,len(test)))
if test['player_fifa_api_id'].iloc[i+1] == test['player_fifa_api_id'].iloc[i]:
test.drop(test.index[i])
有人知道我哪里出错了吗?
这里是数据帧格式的屏幕截图,您应该避免在数据帧中循环。使用矢量化函数通常会有更快、更优雅的解决方案。在您的情况下,筛选所需的行:
player_id = test['player_fifa_api_id']
# if the current row is not equal to the previous row, then keep the current row
keep = player_id != player_id.shift()
# filter for the rows you want to keep
result = test[keep]
您应该避免在数据帧中循环。使用矢量化函数通常会有更快、更优雅的解决方案。在您的情况下,筛选所需的行:
player_id = test['player_fifa_api_id']
# if the current row is not equal to the previous row, then keep the current row
keep = player_id != player_id.shift()
# filter for the rows you want to keep
result = test[keep]
为什么不使用拖放重复项:
import pandas as pd
test.drop_duplicates(subset='player_fifa_api_id', keep='first', inplace=True)
为什么不使用拖放重复项:
import pandas as pd
test.drop_duplicates(subset='player_fifa_api_id', keep='first', inplace=True)
默认情况下,
dataframe.drop
不起作用。因此设置inplace=True
。但是为什么你需要一个for循环呢,df.loc[condition,:]。对于pandas,你不应该使用循环,使用test[test['player\u fifa\u api\u id'!=test['player\u fifa\u api\u id'].shift()
dataframe.drop
默认情况下不起作用。因此设置inplace=True
。但是为什么你需要一个for循环来完成这个任务,df.loc[condition,:]
。对于熊猫,你不应该使用循环,使用test[test['player\u fifa\u api\u id'!=test['player\u fifa\u api\u id'].shift()