Pandas: split column of lists of unequal length into multiple columns

前端 未结 2 535
我在风中等你
我在风中等你 2020-12-07 21:58

I have a Pandas dataframe that looks like the below:

                   codes
1                  [71020]
2                  [77085]
3                  [36415         


        
相关标签:
2条回答
  • 2020-12-07 22:46

    Another solution:

    In [95]: df.codes.apply(pd.Series).add_prefix('code_')
    Out[95]:
        code_0   code_1   code_2
    1  71020.0      NaN      NaN
    2  77085.0      NaN      NaN
    3  36415.0      NaN      NaN
    4  99213.0  99287.0      NaN
    5  99233.0  99233.0  99233.0
    
    0 讨论(0)
  • 2020-12-07 22:47

    Try:

    pd.DataFrame(df.codes.values.tolist()).add_prefix('code_')
    
       code_0   code_1   code_2
    0   71020      NaN      NaN
    1   77085      NaN      NaN
    2   36415      NaN      NaN
    3   99213  99287.0      NaN
    4   99233  99233.0  99233.0
    

    Include the index

    pd.DataFrame(df.codes.values.tolist(), df.index).add_prefix('code_')
    
       code_0   code_1   code_2
    1   71020      NaN      NaN
    2   77085      NaN      NaN
    3   36415      NaN      NaN
    4   99213  99287.0      NaN
    5   99233  99233.0  99233.0
    

    We can nail down all the formatting with this:

    f = lambda x: 'code_{}'.format(x + 1)
    pd.DataFrame(
        df.codes.values.tolist(),
        df.index, dtype=object
    ).fillna('').rename(columns=f)
    
       code_1 code_2 code_3
    1   71020              
    2   77085              
    3   36415              
    4   99213  99287       
    5   99233  99233  99233
    
    0 讨论(0)
提交回复
热议问题