0

Tengo un marco de datos items_sold_summer con identificaciones y me gustaría añadir una columna item_category_id que viene de otro marco de datos items. Así que lo intenté:

items_sold_summer = pd.merge(items_sold_summer, items, on="item_id", how="left")

Pero me devuelve:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-bccff91befa5> in <module>()
      1 transactions_summer_2014 = transactions.loc[transactions.month_year.isin(['06.2014','07.2014','08.2014'])]
      2 items_sold_summer = transactions_summer_2014["item_price"] * transactions_summer_2014["item_cnt_day"]
----> 3 items_sold_summer = pd.merge(items_sold_summer, items, on="item_id", how="left")
      4 items_sold_summer = pd.merge(items_sold_summer, item_categories, on="item_category_name", how="left")
      5 items_sold_summer

/opt/conda/lib/python3.6/site-packages/pandas/core/reshape/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator)
     51                          right_on=right_on, left_index=left_index,
     52                          right_index=right_index, sort=sort, suffixes=suffixes,
---> 53                          copy=copy, indicator=indicator)
     54     return op.get_result()
     55 

/opt/conda/lib/python3.6/site-packages/pandas/core/reshape/merge.py in __init__(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator)
    529             raise ValueError(
    530                 'can not merge DataFrame with instance of '
--> 531                 'type {0}'.format(type(left)))
    532         if not isinstance(right, DataFrame):
    533             raise ValueError(

ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>

data

>>>items_sold_summer.head()
1705909    399.00
1705910    149.00
1705911    246.32
1705912    399.00
1705913    399.00

>>>items.head()
                                           item_name  item_id  \
0          ! ВО ВЛАСТИ НАВАЖДЕНИЯ (ПЛАСТ.)         D        0   
1  !ABBYY FineReader 12 Professional Edition Full...        1   
2      ***В ЛУЧАХ СЛАВЫ   (UNV)                    D        2   
3    ***ГОЛУБАЯ ВОЛНА  (Univ)                      D        3   
4        ***КОРОБКА (СТЕКЛО)                       D        4   

   item_category_id  
0                40  
1                76  
2                40  
3                40  
4                40  

Me gustaria una cosa como:

>>>items_sold_summer.head()
item_id    sum_sold    item_category_id  
1705909    399.00      12
1705910    149.00      23
1705911    246.32      ...
1705912    399.00
1705913    399.00

1 respuesta 1

1

El error indica que el dataframe que intentas mergear es una serie. En tal caso podrías usar .to_frame() en el objeto que es una serie para lograr mergearlo.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.