【问题标题】:Panda having problem merging dataframes together熊猫在将数据框合并在一起时遇到问题
【发布时间】:2021-05-22 08:28:20
【问题描述】:

我正在尝试使用 .merge 和“inner”将两个数据帧合并在一起以找到公共列,这是两个数据帧,第一个,

     Year Month  Brunei Darussalam   ...  Australia   New Zealand   Africa 
0    1978   Jan                 na   ...       28421          3612      587
1    1978   Feb                 na   ...       13982          2521      354
2    1978   Mar                 na   ...       16536          2727      405
3    1978   Apr                 na   ...       16499          3197      736
4    1978   May                 na   ...       20690          5130      514
..    ...   ...                 ...  ...         ...           ...      ...
474  2017   Jul                5625  ...      104873         15358     6964
475  2017   Aug                4610  ...       75171         11197     6987
476  2017   Sep                5387  ...      100987         12021     5458
477  2017   Oct                4202  ...       90940         11834     5635
478  2017   Nov                5258  ...       81821          9348     6717

第二个,

   Year Month
0  1980   Jul
1  1980   Aug
2  1980   Sep
3  1980   Oct
4  1980   Nov

我尝试使用这个输入来初始化我的命令,

merge = pd.merge(dataframe,df, how='inner', on=['Year', 'Month'])
print(merge)

但我不断收到此错误,

Traceback (most recent call last):
  File "main.py", line 52, in <module>
    merge = pd.merge(dataframe,df, how='inner', on=['Year', 'Month'])
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 74, in merge
    op = _MergeOperation(
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 672, in __init__
    self._maybe_coerce_merge_keys()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 1193, in _maybe_coerce_merge_keys
    raise ValueError(msg)
ValueError: You are trying to merge on int64 and object columns. If you wish to proceed you should use pd.concat

【问题讨论】:

    标签: python pandas dataframe merge


    【解决方案1】:

    这意味着一列Year是数字,第二列由字符串填充。

    所以需要相同的类型,例如:

    dataframe['Year'] = dataframe['Year'].astype(int)
    df['Year'] = df['Year'].astype(int)
    df1 = pd.merge(dataframe,df, how='inner', on=['Year', 'Month'])
    

    或者:

    dataframe['Year'] = dataframe['Year'].astype(str)
    df['Year'] = df['Year'].astype(str)
    df1 = pd.merge(dataframe,df, how='inner', on=['Year', 'Month'])
    

    【讨论】:

      猜你喜欢
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 2017-06-08
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 2018-04-16
      相关资源
      最近更新 更多