【问题标题】:i have table formatted data and wanted to convert into tuple format [duplicate]我有表格格式的数据,想转换成元组格式[重复]
【发布时间】:2019-02-01 15:38:28
【问题描述】:

我有一个 csv 文件,如下图所示...

我想在 python 中将整个文件转换为以下格式。

【问题讨论】:

  • 你的csv文件头是什么?
  • 使用tuples = [tuple(x) for x in df.values]

标签: python pandas csv tuples


【解决方案1】:

使用apply()数据框方法的单行

df.apply(lambda x: x.tolist(), axis=1)

df.values.tolist() 也可以使用

【讨论】:

    【解决方案2】:

    您可以在读取您的 CSV 文件正确的文件路径后尝试此操作。

    import pandas as pd
    df = pd.read_csv("path/to/file", names=["Sentence", "Value"])
    result = [(row["Sentence"], row["Value"]) for index, row in df.iterrows()]
    print(result)
    

    【讨论】:

    • 这正是我想要的!谢谢
    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2022-11-18
    相关资源
    最近更新 更多