如何用Pandas检查单元格是否为NaN?

问题描述 投票:0回答:1

我是Pandas的初学者,我想处理一个Excel文件,并计算一个建筑对象(R-R)的米数,尺寸(D)=160mm。

如何从for-slice行的单元格中获取 "IsoOf "列中的数值?df.loc[filt, 'IsoOf'].isnull().values.any() == True

例子

行与'R-R''160'=索引10,12,15,65,70...。df.loc[filt, 'IsoOf'].isnull().values.any() == True 每次检查行0,它没有连接到切片的链接。

我在哪里可以设置 "行"(i)元素检查正确的索引?像 df.loc[filt, 'IsoOf'].isnull(row).values.any() == True

import pandas as pd

#Open file
df = pd.read_excel('Bauteilliste.xlsx')

#edit the display option on jupyter
pd.set_option('display.max_columns', 75)

#Filter 
# 1. All Elements with the ID R-R and the dimension 160mm
filt = (df['KZ'] == 'R-R') & (df['D'] == 160)
#Calculate all the Elements
counter_lenght = 0  #Without Isaltion
counter_lenght_isolation = 0 #With Isaltion

#Get throut every row with the filt filter
for row in df.loc[filt, 'L']:
       #PROBLEM: What todo taht .isnull get the same id from row??
       #It only checks the value .isnull from the index 0 not from the filtered row 
   if df.loc[filt, 'IsoOf'].isnull().values.any() == True:
       counter_lenght = counter_lenght + row
   else:
       counter_lenght_isolation = counter_lenght_isolation + row

print(counter_lenght)
print(counter_lenght_isolation)

Jupyter Notebook的截图

python pandas nan isnull
1个回答
0
投票

试试这样的方法。

import pandas as pd

#Open file
df = pd.read_excel('Bauteilliste.xlsx')

#edit the display option on jupyter
pd.set_option('display.max_columns', 75)

#Filter 
# 1. All Elements with the ID R-R and the dimension 160mm
filt = (df['KZ'] == 'R-R') & (df['D'] == 160)
#Calculate all the Elements
counter_lenght = 0  #Without Isaltion
counter_lenght_isolation = 0 #With Isaltion

#Get throut every row with the filt filter
for row in df.loc[filt, 'L'].iterrows():
       #PROBLEM: What todo taht .isnull get the same id from row??
       #It only checks the value .isnull from the index 0 not from the filtered row 
   if not row[1]['IsoOf']:
       counter_lenght = counter_lenght + row
   else:
       counter_lenght_isolation = counter_lenght_isolation + row

print(counter_lenght)
print(counter_lenght_isolation)

0
投票

我已经找到了解决问题的方法 我将用两个不同的过滤器过滤行。

import pandas as pd


df = pd.read_excel('Bauteilliste.xlsx')

pd.set_option('display.max_columns', 75)

# Filter settings
filt_with_isolation = (df['KZ'] == 'R-R') & (df['D'] == 160) & (df['IsoOf'].isna() == False)
filt_without_isolation = (df['KZ'] == 'R-R') & (df['D'] == 160) & (df['IsoOf'].isna() == True)

# counting the meters
counter_with_isolation = 0
counter_without_isolation = 0 

# for-Slice, get Elements with isolation
for row in df.loc[filt_with_isolation, 'L']:
    counter_with_isolation = counter_with_isolation + row

for row in df.loc[filt_without_isolation, 'L']:
    counter_without_isolation = counter_without_isolation + row

print(counter_with_isolation)
print(counter_without_isolation)

Output:

6030.0
41050.0


© www.soinside.com 2019 - 2024. All rights reserved.