[英]How to replace Specific values of a particular column in Pandas Dataframe based on a certain condition?
I have a Pandas dataframe which contains students and percentages of marks obtained by them. 我有一个Pandas数据框,其中包含学生和他们获得的分数百分比。 There are some students whose marks are shown as greater than 100%.
有些学生的分数显示大于100%。 Obviously these values are incorrect and I would like to replace all percentage values which are greater than 100% by NaN.
显然这些值是不正确的,我想用NaN替换大于100%的所有百分比值。
I have tried on some code but not quite able to get exactly what I would like to desire. 我已经尝试了一些代码,但不能完全得到我想要的东西。
import numpy as np
import pandas as pd
new_DF = pd.DataFrame({'Student' : ['S1', 'S2', 'S3', 'S4', 'S5'],
'Percentages' : [85, 70, 101, 55, 120]})
# Percentages Student
#0 85 S1
#1 70 S2
#2 101 S3
#3 55 S4
#4 120 S5
new_DF[(new_DF.iloc[:, 0] > 100)] = np.NaN
# Percentages Student
#0 85.0 S1
#1 70.0 S2
#2 NaN NaN
#3 55.0 S4
#4 NaN NaN
As you can see the code kind of works but it actually replaces all the values in that particular row where Percentages is greater than 100 by NaN. 正如您可以看到代码类型的工作,但它实际上替换了NaN中Percentages大于100的特定行中的所有值。 I would only like to replace the value in Percentages column by NaN where its greater than 100. Is there any way to do that?
我只想用NaN替换百分比列中的值,其中大于100.有没有办法做到这一点?
Also, 也,
df.Percentages = df.Percentages.apply(lambda x: np.nan if x>100 else x)
or, 要么,
df.Percentages = df.Percentages.where(df.Percentages<100, np.nan)
import numpy as np
import pandas as pd
new_DF = pd.DataFrame({'Student' : ['S1', 'S2', 'S3', 'S4', 'S5'],
'Percentages' : [85, 70, 101, 55, 120]})
#print(new_DF['Student'])
index=-1
for i in new_DF['Percentages']:
index+=1
if i > 100:
new_DF['Percentages'][index] = "nan"
print(new_DF)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.