Split a dataframe column's list into two dataframe columns

前端 未结 2 1487
迷失自我
迷失自我 2021-01-14 16:20

I\'m effectively trying to do a text-to-columns (from MS Excel) action, but in Pandas.

I have a dataframe that contains values like: 1_1, 2_1, 3_1, and I only want t

相关标签:
2条回答
  • 2021-01-14 16:34

    You are close:

    Instead of just splitting try this:

    test2 = pd.DataFrame(test['values'].str.split('_').tolist(), columns = ['c1','c2'])
    
    0 讨论(0)
  • 2021-01-14 16:56

    Use expand=True when doing the split to get multiple columns:

    test['values'].str.split('_', expand=True)
    

    If there's only one underscore, and you only care about the value to the right, you could use:

    test['values'].str.split('_').str[1]
    
    0 讨论(0)
提交回复
热议问题