我有一个 pandas 数据框:
Id Col1
1 ['string']
2 ['string2']
是否可以将数据框转换为另一个类似这样的数据框?
Id Col1
1 string
2 string2
我尝试过这种方式,但只得到[
。
df.col1.apply(lambda x: x[0])
感谢您的宝贵时间!
最佳答案
I tried with this way but only get the [.
那么这意味着它们是字符串
,而不是列表
。您可以通过apply
ast.literal_eval
将它们转换为列表
。然后explode
:
import ast
df.Col1 = df.Col1.apply(ast.literal_eval).explode()
获取
Id Col1
0 1 string
1 2 string2
关于python - 如何取消列出 pandas 列中具有一个值的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67027649/