以下是数据帧的sub_set。

我想删除每一行中所有重复的项。例如,在第一行中,最后一个值dizziness应该被删除,因为在第1行的列WD2中已经存在dizziness。
输出应如下所示:

我知道如何删除列中的重复项,但我不知道如何连续删除重复项。提前谢谢。
发布于 2017-01-27 12:23:50
只需指定另一个轴:
df = df.apply(lambda x:x.drop_duplicates(), axis=1)发布于 2017-01-27 12:17:58
import numpy as np
import pandas as pd
df = pd.DataFrame([
['a', 'b', 'c', np.nan],
['a', 'b', 'b', 'a'],
['c', 'b', 'c', 'd']
])
duplicated = df.apply(lambda x: x.duplicated(keep='first'), axis=1) # converts each row into a pd.Series of True/False values indicating whether a cell is a duplicate
print(duplicated) # this is a pd.DataFrame of True/False indicating which cells to drop.
df[duplicated] = np.nan # assign np.nan to duplicateshttps://stackoverflow.com/questions/41886724
复制相似问题