Fast way to split column into multiple rows in Pandas

后端 未结 3 1485
南笙
南笙 2021-02-02 00:07

I have the following data frame:

import pandas as pd
df = pd.DataFrame({ \'gene\':[\"foo\",
                            \"bar // lal\",
                                  


        
3条回答
  •  半阙折子戏
    2021-02-02 00:31

    Or use:

    df.join(pd.DataFrame(df.gene.str.split(',', expand=True).stack().reset_index(level=1, drop=True)
                    ,columns=['gene '])).drop('gene',1).rename(columns=str.strip).reset_index(drop=True)
    

    Output:

       gene  cell1  cell2
    0   foo      5     12
    1   bar      9     90
    2   lal      9     90
    3   qux      1     13
    4   woz      7     87
    

提交回复
热议问题