簡體   English   中英

如果列的唯一計數大於某個值,則更改 dataframe 中的值

[英]Changing values in dataframe if the unique count of column is greater than a value

IP Routing Banking
1  1        6
2  1        6
3  1        7
3  3        8
4  5        9
5  9        7

對於每一列,如果相同的值出現 2 次或多次,我想將其更改為“其他”。 如何在 pandas python 中執行此操作?

預期 Output:

IP       Routing      Banking
1        Other        Other
2        Other        Other
Other    Other        Other
Other    3            8
4        5            9
5        9            Other
df[df.transform(lambda col: col.duplicated(keep=False), axis=0)] = 'Other'

結果:

      IP Routing Banking
0      1   Other   Other
1      2   Other   Other
2  Other   Other   Other
3  Other       3       8
4      4       5       9
5      5       9   Other

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM