例如,我需要比较数据帧中的两个连续行:
df:
time door name
00:01:10 in alex
00:01:10 in alex
02:01:10 out alex
03:01:10 in alex
04:01:10 out alex
04:01:10 out alex
如果连续两行中door
为in
(或out
),我需要删除重复项。
这是我的代码的一部分:
import pandas as pd
file_name='test.xlsx'
df = pd.read_excel(file_name, header=0, index= False)
mydf = df.sort_values(by='time')
for i in range (len(mydf)):
if (mydf[['door']] != mydf[['door']].shift(-1)).any(axis=1):
print('ok')
else:
print ('nok')
我收到此错误:
if ((mydf[['Door Name']] != mydf[['Door Name']].shift(-1).any(axis=1))):
File "C:\Users\khou\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我不知道如何解决它,任何帮助将不胜感激。
您可以先使用np.where
,然后使用dropna
df['door'] = np.where((df['door'] == df['door'].shift(-1)), np.nan, df['door'])
df.dropna(how='any', axis=0, inplace= True)
print(df)
time door name
00:01:10 in alex
02:01:10 out alex
03:01:10 in alex
04:01:10 out alex
或
并且如果door.values总是有重复的时间,那么您可以简单地将df.drop_duplicates
与参数keep = 'first' and subset = ['time', 'door']
一起使用>
df.drop_duplicates(subset=['time', 'door'], keep='first', inplace= True)
print(df)
time door name
00:01:10 in alex
02:01:10 out alex
03:01:10 in alex
04:01:10 out alex
您可以使用以下方法首先获取索引,然后根据索引删除: