Pandas concat: ValueError: Shape of passed values is blah, indices imply blah2

前端 未结 7 941
梦如初夏
梦如初夏 2020-11-27 16:41

I\'m trying to merge a (Pandas 14.1) dataframe and a series. The series should form a new column, with some NAs (since the index values of the series are a subset of the ind

相关标签:
7条回答
  • 2020-11-27 17:33

    Your indexes probably contains duplicated values.

    import pandas as pd
    
    T1_INDEX = [
        0,
        1,  # <= !!! if I write e.g.: "0" here then it fails
        0.2,
    ]
    T1_COLUMNS = [
        'A', 'B', 'C', 'D'
    ]
    T1 = [
        [1.0, 1.1, 1.2, 1.3],
        [2.0, 2.1, 2.2, 2.3],
        [3.0, 3.1, 3.2, 3.3],
    ]
    
    T2_INDEX = [
        1.2,
        2.11,
    ]
    
    T2_COLUMNS = [
        'D', 'E', 'F',
    ]
    T2 = [
        [54.0, 5324.1, 3234.2],
        [55.0, 14.5324, 2324.2],
        # [3.0, 3.1, 3.2],
    ]
    df1 = pd.DataFrame(T1, columns=T1_COLUMNS, index=T1_INDEX)
    df2 = pd.DataFrame(T2, columns=T2_COLUMNS, index=T2_INDEX)
    
    
    print(pd.concat([pd.DataFrame({})] + [df2, df1], axis=1))
    
    0 讨论(0)
提交回复
热议问题