【发布时间】:2021-08-17 15:25:44
【问题描述】:
我的数据框包含跟随列、“街道”、“州”、“国家”、“邮政编码”
以及行中的一些 NaN 值。
这个表达式df['Street'].isna().sum() 的结果是31,但是这个df['State'].isna().sum() 是204,并且与其余数据类推。
怎么能看出“Street”列的Nan值的行数比其他的少。
我想按州、国家和邮政编码迭代所有数据框,如果我遇到 Nan 值,我会发现是否存在具有相同街道的另一行并填充而不是 Nan 值匹配的 zip/Country/State 值街道。
count = 0
for street, city, state, zip_code in zip(df['Street'], df['City'], df['State'], df['Zip Code']):
if df['City'][count].isna():
location = get_matches(street, df['Street'], df['City'])
if location != None:
df['City'][count] = location
if df['State'][count].isna():
location = get_matches(street, df['Street'], df['State'])
if location != None:
df['State'][count] = location
if df['Zip Code'][count].isna():
location = get_matches(street, df['Street'], df['Zip Code'])
if location != None:
df['Zip Code'][count] = location
count = count + 1
def get_matches(adress,df_street, df_location):
for street, location in zip(df_street, df_location):
if street == adress:
return location
这是我的代码,但它不能正常工作。
【问题讨论】:
-
欢迎来到 Stack Overflow!请包含您的数据的小子集作为可用于测试的可复制代码段以及提供的预期输出数据。请参阅MRE - Minimal, Reproducible, Example 和How to make good reproducible pandas examples 了解更多信息。