【问题标题】:How to fiill Na value in dataframe by get from another rows如何通过从另一行获取来填充数据框中的 Na 值
【发布时间】: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

这是我的代码,但它不能正常工作。

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-20
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多