简体   繁体   English

如何根据特定条件替换Pandas Dataframe中特定列的特定值?

[英]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.有没有办法做到这一点?

Try and use np.where : 尝试并使用np.where

new_DF.Percentages=np.where(new_DF.Percentages.gt(100),np.nan,new_DF.Percentages)

or 要么

new_DF.loc[new_DF.Percentages.gt(100),'Percentages']=np.nan

print(new_DF)

  Student  Percentages
0      S1         85.0
1      S2         70.0
2      S3          NaN
3      S4         55.0
4      S5          NaN

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)

You can use .loc : 你可以使用.loc

new_DF.loc[new_DF['Percentages']>100, 'Percentages'] = np.NaN

Output: 输出:

  Student  Percentages
0      S1         85.0
1      S2         70.0
2      S3          NaN
3      S4         55.0
4      S5          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.

相关问题 如何根据特定条件将数据框中的值替换为另一个数据帧中的值? - How to replace values in a dataframe with values in another dataframe based on certain condition? 如何根据特定条件替换列中的某些值? - How to replace certain values in a column based on a certain condition? 如何根据另一个数据框条件替换数据框列中的值 - How to replace the values in a dataframe column based on another dataframe condition 如何替换熊猫数据框中特定列中的特定值 - How to replace specific values in a specific column in a pandas dataframe 如何根据字典键和值替换熊猫数据框列值? - How to replace pandas dataframe column values based on dictionary key and values? 如何根据条件字典重新计算 DataFrame 列值(Pandas Python) - How to recalculate DataFrame column values based on condition dict (Pandas Python) pandas:如果该值在第二个 dataframe 中,则根据另一个 dataframe 中的条件替换列中的值 - pandas: replace values in a column based on a condition in another dataframe if that value is in the second dataframe 如何根据 python dataframe 中的特定条件将每个单元格值增加一个特定列 - How to increase a particular column each cell value by one, based on a certain condition in python dataframe 如何根据条件替换熊猫列中的字符串? - How to replace a string in pandas column based on a condition? 根据循环熊猫数据框中的条件替换特定值 - Replace specific value based on condition in a loop pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM