csv 如何解决Name错误:未定义名称“df”

nfeuvbwi  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(2137)
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'

此代码使用**pandas模块中的read_csv()**函数阅读CSV文件,并删除第一行、所有包含缺失值的行以及所有包含缺失值的列。
Here is the csv data
Output

1u4esq0p

1u4esq0p1#

名称错误:name 'df' is not defined(名称'df'未定义)错误,因为df DataFrame被重新分配给第10行中的空DataFrame:

df = pd.DataFrame({})

你可以把它去掉

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'

如果现在起作用了就报告,希望能有所帮助。

wkyowqbh

wkyowqbh2#

应为“read_csv”。打字错误。

相关问题