我已经在csv中读到了一个pandas数据帧,它有五列.某些行仅在第二列中具有重复值,我想从数据帧中删除这些行,但drop和drop_duplicates都不起作用.
这是我的实现:
#Read CSV
df = pd.read_csv(data_path, header=0, names=['a', 'b', 'c', 'd', 'e'])
print Series(df.b)
dropRows = []
#Sanitize the data to get rid of duplicates
for indx, val in enumerate(df.b): #for all the values
if(indx == 0): #skip first indx
continue
if (val == df.b[indx-1]): #this is duplicate rtc value
dropRows.append(indx)
print dropRows
df.drop(dropRows) #this doesnt work
df.drop_duplicates('b') #this doesnt work either
print Series(df.b)
Run Code Online (Sandbox Code Playgroud)
当我打印出df.b系列之前和之后它们的长度相同时,我仍然可以看到重复的副本.我的实施有什么问题吗?
Kor*_*rem 17
正如评论中所提到的,drop并drop_duplicates创建了一个新的DataFrame,除非提供了inplace参数.所有这些选项都有效:
df = df.drop(dropRows)
df = df.drop_duplicates('b') #this doesnt work either
df.drop(dropRows, inplace = True)
df.drop_duplicates('b', inplace = True)
Run Code Online (Sandbox Code Playgroud)