如何将熊猫数据框列转换为字符串

时间:2021-03-17 17:53:46

标签: python-3.x dataframe

我正在尝试将 Pandas 数据框列表列转换为字符串。但没有得到预期的输出。谁能帮我解决这个问题:

数据帧:df

    A   B
0   []  ['good']
1   []  ['one','two','three']
2   []  []
3   []  []
4   []  []
5   []  []
6   []  ['yes']
7   []  []
8   []  []
9   []  []
10  []  ['dog','cat']
11  []  []
12  []  []
13  []  []
14  []  ['low','mid','high']

代码行:

df['A'] = df['A'].astype(str)
df['B'] = df['B'].astype(str)

预期输出:

    A   B
0       good
1       one,two,three
2       
3       
4       
5       
6       yes
7       
8       
9       
10      dog,cat
11      
12      
13      
14      low,mid,high

0 个答案:

没有答案