[英]How do i replace the values of a dataframe on a condition
I have a pandas dataframe with more than 50 columns. 我有一个超过50列的pandas数据框。 All the data except the 1st column is float. 除第一列外的所有数据均为浮点型。 I want to replace any value greater than 5.75 with 100. Can someone advise any function to do the same. 我想用100替换任何大于5.75的值。有人可以建议任何函数执行相同的操作。
The replace function is not working as to_value can only take "=" function, and not the greater than function. 替换功能不起作用,因为to_value只能使用“ =”功能,而不能使用大于功能。
这可以使用
df['ColumnName'] = np.where(df['ColumnName'] > 5.75, 100, df['First Season'])
You can make a custom function and pass it to apply: 您可以创建一个自定义函数并将其传递以应用:
import pandas as pd
import random
df = pd.DataFrame({'col_name': [random.randint(0,10) for x in range(100)]})
def f(x):
if x >= 5.75:
return 100
return x
df['modified'] = df['col_name'].apply(f)
print(df.head())
col_name modified
0 2 2
1 5 5
2 7 100
3 1 1
4 9 100
import numpy as np
import pandas as pd
#Create df
np.random.seed(0)
df = pd.DataFrame(2*np.random.randn(100,50))
for col_name in df.columns[1:]: #Skip first column
df.loc[:,col_name][df.loc[:,col_name] > 5.75] = 100
If you have a dataframe: 如果您有数据框:
import pandas as pd
import random
df = pd.DataFrame({'first_column': [random.uniform(5,6) for x in range(10)]})
print(df)
Gives me: 给我:
first_column
0 5.620439
1 5.640604
2 5.286608
3 5.642898
4 5.742910
5 5.096862
6 5.360492
7 5.923234
8 5.489964
9 5.127154
Then check if the value is greater than 5.75: 然后检查该值是否大于5.75:
df[df > 5.75] = 100
print(df)
Gives me: 给我:
first_column
0 5.620439
1 5.640604
2 5.286608
3 5.642898
4 5.742910
5 5.096862
6 5.360492
7 100.000000
8 5.489964
9 5.127154
np.where(df.value > 5.75, 100, df.value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.