Converting a Panda DF List into a string

前端 未结 4 559
悲哀的现实
悲哀的现实 2020-11-30 10:03

I have a panda data frame. One of the columns contains a list. I want that column to be a single string.

For example my list [\'one\',\'two\',\'three\'] should sim

4条回答
  •  春和景丽
    2020-11-30 10:35

    When you cast col to str with astype, you get a string representation of a python list, brackets and all. You do not need to do that, just apply join directly:

    import pandas as pd
    
    df = pd.DataFrame({
        'A': [['a', 'b', 'c'], ['A', 'B', 'C']]
        })
    
    # Out[8]: 
    #            A
    # 0  [a, b, c]
    # 1  [A, B, C]
    
    df['Joined'] = df.A.apply(', '.join)
    
    #            A   Joined
    # 0  [a, b, c]  a, b, c
    # 1  [A, B, C]  A, B, C
    

提交回复
热议问题