【问题标题】:How do I read specific columns in a file and make them into a new csv如何读取文件中的特定列并将它们变成新的 csv
【发布时间】:2022-09-23 00:08:11
【问题描述】:

这是示例 1:

| district_id | date        |
| --------    | ----------- |
| 18          | 1995-03-24  |
| 1           | 1993-02-26  |

样本 2:

| link_id   | type        |
| --------  | ----------- |
| 9         | gold        |
| 19        | classic     |

我想收集样本 1 的日期列和样本 2 的类型列并输出为 data.csv

  • 这两个样本是如何联系起来的?
  • 不需要链接。只需取出 2 列

标签: python pandas


【解决方案1】:

您可以使用数据帧的垂直连接然后渲染它:

df3 = pandas.concat([df1['date'], df2['type']], axis = 1)
df3.to_csv('data.csv')

【讨论】:

  • 这将水平连接它们,您还需要 axis=1 in pd.concat
  • 谢谢,这很有帮助。
猜你喜欢
  • 2019-07-08
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
相关资源
最近更新 更多