使用 pandas 替换多列中的值的优雅而有效的方法
Elegant and efficient way to replace values in multiple columns using pandas
我有一个如下所示的数据框
f = pd.DataFrame({'person_id': [101,101,101,201,201,201,203],
'test_id':[123,123,124,321,321,321,456],
'los_24':[0.3,0.7,0.6,1.01,2,1,2],
'los_48':[1,0.2,0.4,0.7,11,2,3],
'in_24':[21,24,0.3,2.3,0.8,23,1.001],
'in_48':[11.3,202.0,0.2,0.3,41.0,47,2],
'test':['A','B','C','D','E','F','G']})
我想更换all values less than 1 with value 1 under columns like los_24,los_48,in_24,in_48
我尝试了以下
f['los_24'] = np.where((f.los_24 < 1.0),1,f.los_24)
f['los_48'] = np.where((f.los_48 < 1.0),1,f.los_48)
f['in_24'] = np.where((f.in_24 < 1.0),1,f.in_24)
f['in_48'] = np.where((f.in_48 < 1.0),1,f.in_48)
但是你可以看到我用不同的列名多次编写同一行代码。
在实际数据中,我有超过 10 列来替换值。那么,还有其他高效优雅的写法吗?
我希望我的输出如下所示
您可以 select 处理列表中的所有列,并且只调用函数 numpy.where
一次 selected 列:
cols = ['los_24','los_48','in_24','in_48']
f[cols] = np.where((f[cols] < 1.0),1,f[cols])
f[cols] = f[cols].mask((f[cols] < 1.0),1)
person_id test_id los_24 los_48 in_24 in_48 test
0 101 123 1.00 1.0 21.000 11.3 A
1 101 123 1.00 1.0 24.000 202.0 B
2 101 124 1.00 1.0 1.000 1.0 C
3 201 321 1.01 1.0 2.300 1.0 D
4 201 321 2.00 11.0 1.000 41.0 E
5 201 321 1.00 2.0 23.000 47.0 F
6 203 456 2.00 3.0 1.001 2.0 G
你可以clip
:
cols = ["los_24", "los_48", "in_24", "in_48"]
f[cols] = f[cols].clip(lower=1)
获得
person_id test_id los_24 los_48 in_24 in_48 test
0 101 123 1.00 1.0 21.000 11.3 A
1 101 123 1.00 1.0 24.000 202.0 B
2 101 124 1.00 1.0 1.000 1.0 C
3 201 321 1.01 1.0 2.300 1.0 D
4 201 321 2.00 11.0 1.000 41.0 E
5 201 321 1.00 2.0 23.000 47.0 F
6 203 456 2.00 3.0 1.001 2.0 G
哇,给猫剥皮的方法真多..你也可以使用lambda
函数:
cols = ['los_24','los_48','in_24','in_48']
for col in cols:
f[col] = f[col].apply(lambda x: 1 if x<1 else x)
相同的输出:-)
我有一个如下所示的数据框
f = pd.DataFrame({'person_id': [101,101,101,201,201,201,203],
'test_id':[123,123,124,321,321,321,456],
'los_24':[0.3,0.7,0.6,1.01,2,1,2],
'los_48':[1,0.2,0.4,0.7,11,2,3],
'in_24':[21,24,0.3,2.3,0.8,23,1.001],
'in_48':[11.3,202.0,0.2,0.3,41.0,47,2],
'test':['A','B','C','D','E','F','G']})
我想更换all values less than 1 with value 1 under columns like los_24,los_48,in_24,in_48
我尝试了以下
f['los_24'] = np.where((f.los_24 < 1.0),1,f.los_24)
f['los_48'] = np.where((f.los_48 < 1.0),1,f.los_48)
f['in_24'] = np.where((f.in_24 < 1.0),1,f.in_24)
f['in_48'] = np.where((f.in_48 < 1.0),1,f.in_48)
但是你可以看到我用不同的列名多次编写同一行代码。
在实际数据中,我有超过 10 列来替换值。那么,还有其他高效优雅的写法吗?
我希望我的输出如下所示
您可以 select 处理列表中的所有列,并且只调用函数 numpy.where
一次 selected 列:
cols = ['los_24','los_48','in_24','in_48']
f[cols] = np.where((f[cols] < 1.0),1,f[cols])
f[cols] = f[cols].mask((f[cols] < 1.0),1)
person_id test_id los_24 los_48 in_24 in_48 test
0 101 123 1.00 1.0 21.000 11.3 A
1 101 123 1.00 1.0 24.000 202.0 B
2 101 124 1.00 1.0 1.000 1.0 C
3 201 321 1.01 1.0 2.300 1.0 D
4 201 321 2.00 11.0 1.000 41.0 E
5 201 321 1.00 2.0 23.000 47.0 F
6 203 456 2.00 3.0 1.001 2.0 G
你可以clip
:
cols = ["los_24", "los_48", "in_24", "in_48"]
f[cols] = f[cols].clip(lower=1)
获得
person_id test_id los_24 los_48 in_24 in_48 test
0 101 123 1.00 1.0 21.000 11.3 A
1 101 123 1.00 1.0 24.000 202.0 B
2 101 124 1.00 1.0 1.000 1.0 C
3 201 321 1.01 1.0 2.300 1.0 D
4 201 321 2.00 11.0 1.000 41.0 E
5 201 321 1.00 2.0 23.000 47.0 F
6 203 456 2.00 3.0 1.001 2.0 G
哇,给猫剥皮的方法真多..你也可以使用lambda
函数:
cols = ['los_24','los_48','in_24','in_48']
for col in cols:
f[col] = f[col].apply(lambda x: 1 if x<1 else x)
相同的输出:-)