【问题标题】:Transform tabular data from wide to long format using python or C#使用 python 或 C# 将表格数据从宽格式转换为长格式
【发布时间】:2021-07-25 20:03:41
【问题描述】:

我有如下的 csv 文件,

respondant_id 10410 10411 10412
X P12 P13 P14
Y P18 P19 P147

我希望使用 python 或 C# 将其转换为如下所示

respondant_id internal_location ccp
X 10410 P12
X 10411 P13
X 10412 P14
Y 10410 P18
Y 10411 P19
Y 10412 P147

【问题讨论】:

  • 欢迎来到 Stack Overflow。请使用 the tour 了解 Stack Overflow 的工作原理,并阅读 How to Ask 了解如何提高问题的质量。然后查看help center,看看你可以问什么问题。

标签: python c# csv transformation


【解决方案1】:

您可以使用pandasmelt 功能来实现这种转换。如果df 是包含您的数据的pandas DataFrame,那么转换是

df.melt(id_vars = ['respondant_id'], value_vars=['10410', '10411', '10412'], var_name='internal_location', value_name='ccp')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-25
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2015-07-18
    相关资源
    最近更新 更多