【问题标题】:How to convert a dataframe column of type string to list of type tuple如何将字符串类型的数据框列转换为元组类型的列表
【发布时间】:2020-08-03 01:21:51
【问题描述】:

有一个类型为 (str) 的 df


name       password

mark   (('name', 'value passed'),)

cuban  (('location', 'area geocode'),)

(('name', 'value passed'),) 是一个字符串

将 str 类型的数据框列password转换为元组类型的列表

预期输出:

tuples_password = [(('name', 'value passed'),),(('location', 'area geocode'),)]

试过这个:

tuples = [tuple(x) for x in df.password]

【问题讨论】:

  • 你能详细解释一下你的预期输出吗?
  • @Mari 编辑了问题

标签: python python-3.x pandas list tuples


【解决方案1】:

我是个菜鸟,虽然我想试试看。

df['tuple_field'] = [tuple(eval(i)) for i in df['password']]
tuples = [x for x in df['tuple_field']]

输出:

[(('name', 'value passed'),), (('location', 'area geocode'),)]

【讨论】:

    猜你喜欢
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多