add columns different length pandas

后端 未结 4 1757
闹比i
闹比i 2020-12-02 09:22

I have a problem with adding columns in pandas. I have DataFrame, dimensional is nxk. And in process I wiil need add columns with dimensional mx1, where m = [1,n], but I don

相关标签:
4条回答
  • 2020-12-02 09:56

    If you use accepted answer, you'll lose your column names, as shown in the accepted answer example, and described in the documentation (emphasis added):

    The resulting axis will be labeled 0, ..., n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information.

    It looks like column names ('Name column') are meaningful to the Original Poster / Original Question.

    To save column names, use pandas.concat, but don't ignore_index (default value of ignore_index is false; so you can omit that argument altogether). Continue to use axis=1:

    import pandas
    
    # Note these columns have 3 rows of values:
    original = pandas.DataFrame({
        'Age':[10, 12, 13], 
        'Gender':['M','F','F']
    })
    
    # Note this column has 4 rows of values:
    additional = pandas.DataFrame({
        'Name': ['Nate A', 'Jessie A', 'Daniel H', 'John D']
    })
    
    new = pandas.concat([original, additional], axis=1) 
    # Identical:
    # new = pandas.concat([original, additional], ignore_index=False, axis=1) 
    
    print(new.head())
    
    #          Age        Gender        Name
    #0          10             M      Nate A
    #1          12             F    Jessie A
    #2          13             F    Daniel H
    #3         NaN           NaN      John D
    

    Notice how John D does not have an Age or a Gender.

    0 讨论(0)
  • 2020-12-02 09:57

    Use concat and pass axis=1 and ignore_index=True:

    In [38]:
    
    import numpy as np
    df = pd.DataFrame({'a':np.arange(5)})
    df1 = pd.DataFrame({'b':np.arange(4)})
    print(df1)
    df
       b
    0  0
    1  1
    2  2
    3  3
    Out[38]:
       a
    0  0
    1  1
    2  2
    3  3
    4  4
    In [39]:
    
    pd.concat([df,df1], ignore_index=True, axis=1)
    Out[39]:
       0   1
    0  0   0
    1  1   1
    2  2   2
    3  3   3
    4  4 NaN
    
    0 讨论(0)
  • 2020-12-02 10:00

    We can add the different size of list values to DataFrame.

    Example

    a = [0,1,2,3]
    b = [0,1,2,3,4,5,6,7,8,9]
    c = [0,1]
    

    Find the Length of all list

    la,lb,lc = len(a),len(b),len(c)
    # now find the max
    max_len = max(la,lb,lc)
    

    Resize all according to the determined max length (not in this example

    if not max_len == la:
      a.extend(['']*(max_len-la))
    if not max_len == lb:
      b.extend(['']*(max_len-lb))
    if not max_len == lc:
      c.extend(['']*(max_len-lc))
    

    Now the all list is same length and create dataframe

    pd.DataFrame({'A':a,'B':b,'C':c}) 
    

    Final Output is

       A  B  C
    0  1  0  1
    1  2  1   
    2  3  2   
    3     3   
    4     4   
    5     5   
    6     6   
    7     7   
    8     8   
    9     9  
    
    0 讨论(0)
  • 2020-12-02 10:01

    I had the same issue, two different dataframes and without a common column. I just needed to put them beside each other in a csv file.

    • Merge: In this case, "merge" does not work; even adding a temporary column to both dfs and then dropping it. Because this method makes both dfs with the same length. Hence, it repeats the rows of the shorter dataframe to match the longer dataframe's length.
    • Concat: The idea of The Red Pea didn't work for me. It just appended the shorter df to the longer one (row-wise) while leaving an empty column (NaNs) above the shorter df's column.
    • Solution: You need to do the following:
    df1 = df1.reset_index()
    df2 = df2.reset_index()
    df = [df1, df2]
    df_final = pd.concat(df, axis=1)
    
    df_final.to_csv(filename, index=False)
    

    This way, you'll see your dfs besides each other (column-wise), each of which with its own length.

    0 讨论(0)
提交回复
热议问题