Search code examples
pythonpandasnanisnull

How do i check a cell if NaN with pandas?


I'am a beginner in Pandas. I want to manipulat a Excel File and count the Meters of an Construction object (R-R) with the Dimension (D) = 160mm.

How can i get the value in the column 'IsoOf' from the cell in the row of the for-slice? df.loc[filt, 'IsoOf'].isnull().values.any() == True

Example

Row with 'R-R' '160' = Index 10,12,15,65,70.... df.loc[filt, 'IsoOf'].isnull().values.any() == True checks every time the Row 0 it has no link to the for Slice

where can i set the "row" (i) Element to check the right Index? Like 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)

Screenshot from Jupyter Notebook


Solution

  • I have found a solution to my problem. I will filter the lines with two different filters.

    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