Python 3.x 代替较少发生的其他事件
代替较少发生的其他 在其中一列中,即数据框的“名称”,如df,我有如下数据: 样本输入:Python 3.x 代替较少发生的其他事件,python-3.x,replace,Python 3.x,Replace,代替较少发生的其他 在其中一列中,即数据框的“名称”,如df,我有如下数据: 样本输入: A 3 B 2 Others 2 名字 A A A B B C D 我需要以下格式的输出 预期输出: A 3 B 2 Others 2 欢迎使用python3中的任何代码。提前感谢。您需要: x = list(df['name'].value_counts()[:2].index) # This will fetch top N values df['name'] = np.w
A 3
B 2
Others 2
名字
A
A
A
B
B
C
D
我需要以下格式的输出
预期输出:
A 3
B 2
Others 2
欢迎使用python3中的任何代码。提前感谢。您需要:
x = list(df['name'].value_counts()[:2].index) # This will fetch top N values
df['name'] = np.where(df['name'].isin(x), df['name'], 'others')
print(df['Name'].value_counts())
输出
A 3
B 2
others 2