Home > Net >  How to solve NameError: name 'df' is not defined
How to solve NameError: name 'df' is not defined

Time:01-06

import pandas as pd

df = pd.read_csv('JOB205DAYREP.csv', header=0)
df = df.drop([0],axis=0)
df = df.dropna(axis=0, how='all')
df = df.dropna(axis=1, how='all')

df.head(10)

df = pd.DataFrame({})

from dateutil.parser import parse
for index, row in df.iterrows():
    if pd.isnull(row['FLT']) or row ['FLT'] == 'NA':
        df.at[index, 'FLT'] = df.at[index-1, 'FLT']
    
    if pd.isnull(row['STD']) or row ['STD'] == 'NA':
        df.at[index,'STD'] = df.at[index-1, 'STD']

    else:
        row['STD'] = fltdate   row ['STD']
    if not index==0 :
        if pd.isnull(row['VIAOFF']) or row['VIAOFF'] == 'NA':
            df.at[index, 'VIAOFF'] = df.at[index-1, 'VIAOFF']
        if pd.isnull(row['DEP']) or row['DEP'] == 'NA':
            df.at[index, 'DEP'] = df.at[index-1, 'DEP']
df.head(10)

from dateutil.parser import parse
date_string = '21/01/2023'
try :
    if parse(date_string, fuzzy=True, dayfirst=True) is not None:
        print('The string is a valid date.')
    else:
        print('The string is not a valid date.')
except Exception as e:
    error ='error'

This code is reading in a CSV file using the read_csv() function from the pandas module, and dropping the first row, any rows with all missing values, and any columns with all missing values.

Here is the csv data

Output

CodePudding user response:

The NameError: name 'df' is not defined error is raised because the df DataFrame is being re-assigned to an empty DataFrame in line #10:

df = pd.DataFrame({})

you can solve it removing that,

import pandas as pd

df = pd.read_csv('JOB205DAYREP.csv', header=0)
df = df.drop([0],axis=0)
df = df.dropna(axis=0, how='all')
df = df.dropna(axis=1, how='all')

for index, row in df.iterrows():
    if pd.isnull(row['FLT']) or row ['FLT'] == 'NA':
        df.at[index, 'FLT'] = df.at[index-1, 'FLT']
    
    if pd.isnull(row['STD']) or row ['STD'] == 'NA':
        df.at[index,'STD'] = df.at[index-1, 'STD']

    else:
        row['STD'] = fltdate   row ['STD']
    if not index==0 :
        if pd.isnull(row['VIAOFF']) or row['VIAOFF'] == 'NA':
            df.at[index, 'VIAOFF'] = df.at[index-1, 'VIAOFF']
        if pd.isnull(row['DEP']) or row['DEP'] == 'NA':
            df.at[index, 'DEP'] = df.at[index-1, 'DEP']

# Check if a string is a real valid date
from dateutil.parser import parse
date_string = '21/01/2023'
try :
    if parse(date_string, fuzzy=True, dayfirst=True) is not None:
        print('The string is a valid date.')
    else:
        print('The string is not a valid date.')
except Exception as e:
    error ='error'

Report back if it works now, hope it helps.

CodePudding user response:

It should be "read_csv". Typo error.

  • Related