Pandas concat yields ValueError: Plan shapes are not aligned

后端 未结 6 710
忘掉有多难
忘掉有多难 2020-12-03 02:23

In pandas, I am attempting to concatenate a set of dataframes and I am getting this error:

ValueError: Plan shapes are not aligned

My underst

6条回答
  •  天命终不由人
    2020-12-03 03:09

    How to reproduce above error from pandas.concat(...):

    ValueError: Plan shapes are not aligned

    The Python (3.6.8) code:

    import pandas as pd
    df = pd.DataFrame({"foo": [3] })
    print(df)
    df2 = pd.concat([df, df], axis="columns")
    print(df2)
    df3 = pd.concat([df2, df], sort=False) #ValueError: Plan shapes are not aligned
    

    which prints:

       foo
    0    3
    
       foo  foo
    0    3    3
    ValueError: Plan shapes are not aligned
    

    Explanation of error

    If the first pandas dataframe (here df2) has a duplicate named column and is sent to pd.concat and the second dataframe isn't of the same dimension as the first, then you get this error.

    Solution

    Make sure there are no duplicate named columns:

    df_onefoo = pd.DataFrame({"foo": [3] })
    print(df_onefoo)
    df_onebar = pd.DataFrame({"bar": [3] })
    print(df_onebar)
    df2 = pd.concat([df_onefoo, df_onebar], axis="columns")
    print(df2)
    df3 = pd.concat([df2, df_onefoo], sort=False)
    print(df2)
    

    prints:

       foo
    0    3
    
       bar
    0    3
    
       foo  bar
    0    3    3
    
       foo  bar
    0    3    3
    

    Pandas concat could have been more helpful with that error message. It's a straight up bubbleup-implementation-itis, which is textbook python.

提交回复
热议问题