在大熊猫数据框中替换大于 1 的值

Mat*_*her 3 python dataframe pandas

我试图用 1 替换所有大于 1 的数字,同时以最小的努力在整个数据框中保持原始 1 和 0 不变。任何支持表示赞赏!

我的数据框看起来像这样,但包含更多的列和行。

Report No   Apple   Orange   Lemon   Grape   Pear
One           5       0        2       1      1
Two           1       1        0       3      2
Three         0       0        2       1      3
Four          1       1        3       0      0
Five          4       0        0       1      1
Six           1       3        1       2      0
Run Code Online (Sandbox Code Playgroud)

期望输出:

Report No   Apple   Orange   Lemon   Grape   Pear
One           1       0        1       1      1
Two           1       1        0       1      1
Three         0       0        1       1      1
Four          1       1        1       0      0
Five          1       0        0       1      1
Six           1       1        1       1      0
Run Code Online (Sandbox Code Playgroud)

Ch3*_*teR 5

你可以试试这个。

df.set_index('Report No',inplace=True)
df[df>1]=1
df.reset_index()

Report No   Apple   Orange   Lemon   Grape   Pear
One           1       0        1       1      1
Two           1       1        0       1      1
Three         0       0        1       1      1
Four          1       1        1       0      0
Five          1       0        0       1      1
Six           1       1        1       1      0
Run Code Online (Sandbox Code Playgroud)

或者,如果您有一些非数字列,请使用它。无需使用set_indexreset_index。这相当于df.select_dtypes('number')

val = df._get_numeric_data()
val[val>1] = 1
df
Report No   Apple   Orange   Lemon   Grape   Pear
One           1       0        1       1      1
Two           1       1        0       1      1
Three         0       0        1       1      1
Four          1       1        1       0      0
Five          1       0        0       1      1
Six           1       1        1       1      0
Run Code Online (Sandbox Code Playgroud)

或使用 df.mask

df.set_index('Report No',inplace=True)
df.mask(df>1,1).reset_index()
Report No   Apple   Orange   Lemon   Grape   Pear
One           1       0        1       1      1
Two           1       1        0       1      1
Three         0       0        1       1      1
Four          1       1        1       0      0
Five          1       0        0       1      1
Six           1       1        1       1      0
Run Code Online (Sandbox Code Playgroud)

或使用 np.where

df[df.columns[1:]] = df.iloc[:,1:].where(df.iloc[:,1:] >1 ,1)
Run Code Online (Sandbox Code Playgroud)

或者np.select在处理多个条件时使用它会很有帮助。如果要将小于 0 的值转换为 0,将大于 1 的值转换为 1。

df.set_index('Report No',inplace=True)
condlist = [df>=1,df<=0] #you can have more conditions and add choices accordingly.
choice = [1,0] #len(condlist) should be equal to len(choice).
df.loc[:] = np.select(condlist,choice)
Run Code Online (Sandbox Code Playgroud)

就像 Jan 提到的使用 df.clip


不推荐,但你可以试试这个很有趣。使用df.astype.

df.set_index('Report No',inplace=True)
df.astype('bool').astype('int')
Run Code Online (Sandbox Code Playgroud)

注意:此方法仅转换falsyFalsetruthyTrue,即这将转换0False比其他任何事情0True甚至负数。

s = pd.Series([1,-1,0])
s.astype('bool')
0     True
1     True
2    False
dtype: bool

s.astype('bool').astype('int')
0    1
1    1
2    0
dtype: int32
Run Code Online (Sandbox Code Playgroud)