Python “如何比较一个值”;“南”;熊猫从到
我对熊猫图书馆有意见。。。我使用pandas.to_numeric将列与值进行比较,但是,当原始值不是“数字相似”时,pd.to_numeric将csv单元格转换为“nan”值,但我不知道如何在“if”中比较此“nan”值。。。 基本上,如果单元格的值为nan,我需要在正确的位置捕捉单元格,如下所示:Python “如何比较一个值”;“南”;熊猫从到,python,pandas,csv,type-conversion,Python,Pandas,Csv,Type Conversion,我对熊猫图书馆有意见。。。我使用pandas.to_numeric将列与值进行比较,但是,当原始值不是“数字相似”时,pd.to_numeric将csv单元格转换为“nan”值,但我不知道如何在“if”中比较此“nan”值。。。 基本上,如果单元格的值为nan,我需要在正确的位置捕捉单元格,如下所示: while counter < len(df): #df is my pandas Dataframe if df.values[counter][2].isnull():
while counter < len(df): #df is my pandas Dataframe
if df.values[counter][2].isnull(): #[2] is my column with "possible nan values, but not works!"
if 0 <= df.values[counter][4] <= 100: #[4] is my column with "number values"
df.values[counter][2] = df.values[counter][4]
df.values[counter][1] = df.values[counter][3] #[3] Like column [4]
counter += 1
您可以看到我的原始CSV文件在|…|中有列我不使用,但是,当我在BT_CPY列中使用_numeric时,行被移位,值法线在“nan”中转换,我需要的是验证这个nan,如果nan==真,我需要捕捉BT_健康值并放在BT_CPY列上 我不明白你的问题 但要将值与“NaN”进行比较,请在if条件中使用math.isnan()。
这对我总是有用:)你能给我们看看你的数据集吗?@Shrey当然,给我一秒钟!
df[BCY] = pd.to_numeric(df[BCY], errors='coerce') #BCY and others is the name of column
df[BCN] = pd.to_numeric(df[BCN], errors='coerce')
df[BPD] = pd.to_numeric(df[BPD], errors='coerce')
df[BHT] = pd.to_numeric(df[BHT], errors='coerce')