【问题标题】:How to map one DataFrame onto another with different dimensions如何将一个 DataFrame 映射到另一个不同维度的 DataFrame
【发布时间】:2021-04-02 18:11:27
【问题描述】:

如果已经有人问过这个问题,我深表歉意,我已经搜索了解决方案并发现了类似的问题,但没有解决我的问题。

我有 2 个数据帧

df2.head()   

                    Location        lat         lon
0          Albury, Australia -36.080477  146.916280
1  Badgerys Creek, Australia -33.881667  150.744163
2           Cobar, Australia -31.498333  145.834444
3   Coffs Harbour, Australia -30.296241  153.113529
4           Moree, Australia -29.461720  149.840715


df2.shape
(49, 3)

df1.info()

RangeIndex: 145460 entries, 0 to 145459
Data columns (total 21 columns):
 #   Column         Non-Null Count   Dtype  
---  ------         --------------   -----  
 0   Date           145460 non-null  object 
 1   Location       145460 non-null  object 
 2   MinTemp        143975 non-null  float64
 3   MaxTemp        144199 non-null  float64
 4   Rainfall       142199 non-null  float64
 5   WindGustDir    135134 non-null  object 
 6   WindGustSpeed  135197 non-null  float64
 7   WindDir9am     134894 non-null  object 
 8   WindDir3pm     141232 non-null  object 
 9   WindSpeed9am   143693 non-null  float64
 10  WindSpeed3pm   142398 non-null  float64
 11  Humidity9am    142806 non-null  float64
 12  Humidity3pm    140953 non-null  float64
 13  Pressure9am    130395 non-null  float64
 14  Pressure3pm    130432 non-null  float64
 15  Temp9am        143693 non-null  float64
 16  Temp3pm        141851 non-null  float64
 17  RainToday      142199 non-null  object 
 18  RainTomorrow   142193 non-null  object 
 19  Country        145460 non-null  object 
 20  ApLocation     145460 non-null  object 
dtypes: float64(12), object(9)

df1 具有 49 个唯一的位置值,df2 具有具有相应经度和纬度值的位置。

我想将经度和纬度值映射到df1中的相应位置。

【问题讨论】:

  • 一种方法是将 2 个 dfs 合并到一个公共列上,如本例中的“位置”。试试df1.merge(df2,how='left',on='Location')

标签: python pandas


【解决方案1】:

尝试合并 2 个数据框,正如 sophocles 在他的编辑中指出的那样。 如果您想了解有关此主题的更多信息,可以在此处查看现有页面:Pandas Merging 101

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 2021-10-30
    • 1970-01-01
    • 2016-02-06
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多