I have a data frame with different values in a column x. I want to reset the values that appear only once in a column.
x
So this is:
x 1 10 2 30 3 30 4 40 5 40 6 50
You should get the following:
x 2 30 3 30 4 40 5 40
I was wondering if there is a way to do this.
You can easily get this using groupbyand transform:
groupby
transform
In [1]: import pandas as pd In [2]: df = pd.DataFrame([10, 30, 30, 40, 40, 50], columns=['x']) In [3]: df = df[df.groupby('x').x.transform(len) > 1] In [4]: df Out[4]: x 1 30 2 30 3 40 4 40
You can use groupbyand then filterit:
filter
In [9]: df = pd.DataFrame([10, 30, 30, 40, 40, 50], columns=['x']) df = df.groupby('x').filter(lambda x: len(x) > 1) df Out[9]: x 1 30 2 30 3 40 4 40
:
df = df.loc[df.duplicated(subset='x', keep=False), :]
, :
df = df.loc[~df.duplicated(subset='x', keep=False), :]
df = df.loc[~df.duplicated(subset='x'), :]
df = df.drop_duplicates(subset='x')
Source: https://habr.com/ru/post/1611212/More articles:https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1611207/no-rfcomm-connection-on-raspberry-pi-while-same-setup-on-desktop-works&usg=ALkJrhjdZVYnBwloj5V8Vtt36Zm13D0ESwPython - random child name generator generators - (duplicate input, calling variables) - pythonMac should be called exactly 1 time, but called 0 times - phpCould not parse .csv file downloaded using Flask - pythonReboot or merge to upgrade to the last wizard, if not - gitПочему уточнения Ruby изменяют только классы, а не модули? - rubyChess table template (spaces and zeros) does not work with jQuery - javascriptOverriding iOS storyboard restrictions - iosRVM: error installing the latest version of ruby on Mac OSX El Capitan - ruby | fooobar.comImageIO in Java - javaAll Articles