Pandas concat failing

后端 未结 4 624
无人及你
无人及你 2020-12-03 10:24

I am trying to concat dataframes based on the foll. 2 csv files:

df_a: https://www.dropbox.com/s/slcu7o7yyottujl/df_current.csv?dl=0

df_b: https://www.dropbo

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 10:53

    Unfortunately, the source files are already unavailable, so I can't check my solution in your case. In my case the error occurred when:

    1. Data frames have two columns with the same name (I've had ID and id columns, which I then converted to lower case, so they become the same)
    2. Value types of the same-named columns are different

    Here is an example which gives me the error in question:

    df1 = pd.DataFrame(data=[
        ['a', 'b', 'id', 1],
        ['a', 'b', 'id', 2]
    ], columns=['A', 'B', 'id', 'id'])
    
    df2 = pd.DataFrame(data=[
        ['b', 'c', 'id', 1],
        ['b', 'c', 'id', 2]
    ], columns=['B', 'C', 'id', 'id'])
    pd.concat([df1, df2])
    >>> AssertionError: Number of manager items must equal union of block items
     # manager items: 4, # tot_items: 5
    

    Removing / renaming one of the columns makes this code work.

提交回复
热议问题