【问题标题】:Pandas: joining specific columns of one data frame to another [duplicate]熊猫:将一个数据帧的特定列连接到另一个[重复]
【发布时间】:2018-08-28 17:57:52
【问题描述】:

我在 pandas 中有 2 个以下数据框:

电影

+---+------------------------------+--------------+-----------+
|   | movie title                  | genre        | tconst    |
+---+------------------------------+--------------+-----------+
| 0 | Edison Kinetoscopic Record   | Documentary  | tt0000008 |
+---+------------------------------+--------------+-----------+
| 1 | La sortie des usines Lumière | Documentary  | tt0000010 |
+---+------------------------------+--------------+-----------+
| 2 | The Arrival of a Train       | Documentary  | tt0000012 |
+---+------------------------------+--------------+-----------+
| 3 | The Oxford and Cambridge     | NaN          | tt0000025 |
+---+------------------------------+--------------+-----------+
| 4 | Le manoir du diable          | Short|Horror | tt0000091 |
+---+------------------------------+--------------+-----------+

船员

+---+-----------+-----------+---------+------+
|   | tconst    | directors | writers | year |
+---+-----------+-----------+---------+------+
| 0 | tt0000001 | nm0005690 | \N      | 2001 |
+---+-----------+-----------+---------+------+
| 1 | tt0000002 | nm0721526 | \N      | 2002 |
+---+-----------+-----------+---------+------+
| 2 | tt0000003 | nm0721526 | \N      | 2003 |
+---+-----------+-----------+---------+------+
| 3 | tt0000004 | nm0721526 | \N      | 2004 |
+---+-----------+-----------+---------+------+
| 4 | tt0000005 | nm0005690 | \N      | 2005 |
+---+-----------+-----------+---------+------+

如何创建一个新的数据框,将 directorsyear 列仅加入 movies 数据框(使用 tconst 列)?

【问题讨论】:

  • 你能重新格式化帖子以正确显示你的df吗?它很难遵循,它可能会让人们很容易帮助你。
  • 抱歉,我确信我做的一切都是正确的,但桌子看起来仍然很糟糕
  • 下面的答案很好!

标签: python pandas dataframe merge


【解决方案1】:

试试:

pd.merge(movies, crew[["tconst", "directors", "year"]], on="tconst", how="left")

on 参数告诉函数您要在键 tconst 上合并,how 参数告诉函数您要如何处理两个 DataFrame 之间不是交叉(共享)的行.

【讨论】:

  • 谢谢!正是我想要的。
猜你喜欢
  • 2018-08-18
  • 1970-01-01
  • 2022-10-23
  • 2020-05-12
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 2018-01-22
  • 2020-02-04
相关资源
最近更新 更多