为什么数据框输入和数字标准输入之间存在不一致?

Why is there an incongruence between dataframe input and numeric standard input?

nomi = pgeocode.Nominatim('nl')
nomi.query_postal_code("1081")

Out:postal_code                1081
    country_code                 NL
    place_name            Amsterdam
    state_name        Noord-Holland
    state_code                    7
    county_name           Amsterdam
    county_code                 363
    community_name              NaN
    community_code              NaN
    latitude                52.3278
    longitude                 4.862
    accuracy                      6
    Name: 0, dtype: object

我有以下 df 列:

     Postalcodestring
0    41460
1     7411
2      NaN
Name: Postalcodestring, dtype: object

当我输入

df['City1'] = nomi.query_postal_code(1081)['place_name']
 
     City1
0    Amsterdam
1    Amsterdam
2    Amsterdam

但是,当我输入以下代码适配df栏时:

df['City1'] = nomi.query_postal_code('Postalcodestring')['place_name']
0   NaN
1   NaN
2   NaN
Name: City1, dtype: float64

出了什么问题?

以下内容可能对您有所帮助。

模块:

import pandas as pd
import numpy as np
import pgeocode

数据:

df = pd.DataFrame({'Postalcodestring':[41460, 7411, np.nan]}, dtype='int32')

请注意,第一个邮政编码 return 是 NAN(荷兰不存在 41460),第三个邮政编码的输入是 NAN,因此也应该 return 是 NAN。

以下 returns [NAN, "Deventer", NAN]

df['City1'] = df['Postalcodestring'].apply(lambda x: nomi.query_postal_code(int(x))['place_name'] if(pd.notnull(x)) else x)