导出为CSV时从DataFrame列中删除括号 [英] Removing brackets from a DataFrame column when exporting to CSV

查看:148
本文介绍了导出为CSV时从DataFrame列中删除括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列具有这样的值:

I have a column with values like this:

columnA
[12,4352,545]
[123123,5436,665]
[234,646,5747]

当我将包含此列的DataFrame写入CSV时,我想删除该列中每个数组的括号.我已经尝试过 str.replace str.strip ,但是花括号从未删除.我也尝试将它们全部转换为元组,然后改为删除括号,但无济于事.

And when I write the DataFrame containing this column to a CSV, I want to remove the brackets around each array in the column. I've tried str.replace and str.strip, but the braces are never removed. I've also tried converting them all to tuples and then removing the parentheses instead, to no avail.

推荐答案

我建议使用与逗号不同的分隔符.您可以使用任何您想要的东西.

I'd recommend a different delimiter than comma. You can use whatever you want though.

df = pd.DataFrame({'colA':[[1,2],[3,4]]})


应用地图

#                       The Delimiter ▼
df.assign(colA=df.colA.map(lambda x: '|'.join(map(str, x))))
  colA
0  1|2
1  3|4

这篇关于导出为CSV时从DataFrame列中删除括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆