【问题标题】:Extracting data from a dataframe with dictionaries in column elements使用列元素中的字典从数据框中提取数据
【发布时间】:2020-08-04 14:48:49
【问题描述】:

我尝试将列提取到列表中,然后获取值,但没有发生。我还需要保留 NaN。 该列有一个结构 -

df['geocoded_column']
0       {'type': 'Point', 'coordinates': [-117.009178,...
1                                                     NaN
2                                                     NaN
3       {'type': 'Point', 'coordinates': [-104.612123,...
4       {'type': 'Point', 'coordinates': [-86.043072, ...
                              ...                        
2726    {'type': 'Point', 'coordinates': [-87.879139, ...
2727    {'type': 'Point', 'coordinates': [-95.266593, ...
2728    {'type': 'Point', 'coordinates': [-92.317039, ...
2729    {'type': 'Point', 'coordinates': [-83.017686, ...
2730    {'type': 'Point', 'coordinates': [-91.528653, ...

df['geocoded_column'][0] - 
{'type': 'Point', 'coordinates': [-117.009178, 32.779435]}



I need to segregate it into the type - 
Latitude                        Longitude
-117.117.009178                32.779435
NaN                             NaN

【问题讨论】:

    标签: python python-3.x pandas list dataframe


    【解决方案1】:

    这应该对你有用

    df['Latitude'] = [point['coordinates'][0] for point in df['geocoded_column']]
    df['Longitude'] = [point['coordinates'][1] for point in df['geocoded_column']]
    

    【讨论】:

    • 为了避免理解两次:df['Lat'], df['Lon'] = zip(*[d['coordinates'] for d in df['geocoded_column']])
    • 不处理 NaN
    • df['Lat'], df['Lon'] = zip(*[(np.nan, np.nan) if pd.isna(d) else d['coordinates'] for d in df['geocoded_column']])
    【解决方案2】:

    勾选此项,Nan值受控:

    lat = []
    lon = []
    for x in df['geocoded_column']:
        if isinstance(x,dict):
            lat.append(x['coordinates'][0])
            lon.append(x['coordinates'][1])
        else:
            lat.append(np.nan)
            lon.append(np.nan)
    
    df['latitude']=lat
    df['longitude']=lon
    

    输出:

                                         geocoded_column    latitude  longitude
    0  {'type': 'Point', 'coordinates': [-117.009178,... -117.009178      -33.0
    1                                                NaN         NaN        NaN
    2                                                NaN         NaN        NaN
    3  {'type': 'Point', 'coordinates': [-104.612123,... -104.612123      123.0
    4  {'type': 'Point', 'coordinates': [-86.043072, ...  -86.043072       24.0
    

    【讨论】:

    • 这对我有用!非常感谢:)
    猜你喜欢
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2021-09-10
    • 2021-10-31
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多