【问题标题】:Python: Extracting information from one dataframe to another (with different lengths) based on conditionPython:根据条件将信息从一个数据帧提取到另一个数据帧(具有不同的长度)
【发布时间】:2021-10-06 18:04:16
【问题描述】:

我有两个长度不同的数据帧(df1 和 df2)(25696 和 28)。尽管如此,它们有两列共享相同的信息(date 和 day_period)。

以下是数据预览:

df1 
>>
    id timestamp                 col1   col2    day_period  date
0    A  2021-06-09 08:12:18.000  12     32      Morning     2021-06-09
1    A  2021-06-09 08:12:18.000  5      32      Morning     2021-06-09
2    A  2021-06-09 08:12:18.587  54     34      Morning     2021-06-09
3    A  2021-06-09 08:12:18.716  56     53      Morning     2021-06-09 
4    A  2021-06-09 08:12:33.000  34     23      Morning     2021-06-09


df2
>>
    date       day_period   temperature atmospheric_pressure    wind_speed  humidity
0   2021-06-09  Night       15          30.1                    2.6         94
1   2021-06-09  Morning     14          30.1                    3.2         90
2   2021-06-09  Day         18          30.1                    4.2         60
3   2021-06-09  Evening     19          30.0                    2.7         66
4   2021-06-10  Night       16          30.0                    3.6         81

我的目标是在 day_period 和 date 相同时将 df2 的所有其他列合并到 df1。在 df1 中预览的情况下,对于日期为 2021-06-09 和 day_period 早上。考虑到长度的差异,如何做到这一点?

所以,我的目标输出是这样的:

    id timestamp                 col1   col2    day_period  date        temperature    atmospheric_pressure  wind_speed  humidity
0    A  2021-06-09 08:12:18.000  12     32      Morning     2021-06-09  15             30.1                  2.6           94
1    A  2021-06-09 08:12:18.000  5      32      Morning     2021-06-09  14             30.1                  3.2           90
2    A  2021-06-09 08:12:18.587  54     34      Morning     2021-06-09  18             30.1                  4.2           60
3    A  2021-06-09 08:12:18.716  56     53      Morning     2021-06-09  19             30.0                  2.7           66
4    A  2021-06-09 08:12:33.000  34     23      Morning     2021-06-09  16             30.0                  3.6           81

请注意,df1 中有一个 ID 列,因此日期仅按每个 ID 的时间顺序排列。

【问题讨论】:

  • 我想在['day_period','date']上合并两个df
  • 您能展示一下最终结果的样子吗?
  • 刚刚编辑了问题以添加所需的输出。

标签: python pandas dataframe merge concatenation


【解决方案1】:

您想加入,例如使用merge

df1.merge(df2, left_on=['date', 'day_period'], right_on=['date', 'day_period'])

输出:

  id                timestamp  col1  col2 day_period        date  temperature  atmospheric_pressure  wind_speed  humidity
0  A  2021-06-09 08:12:18.000    12    32    Morning  2021-06-09           14                  30.1         3.2        90
1  A  2021-06-09 08:12:18.000     5    32    Morning  2021-06-09           14                  30.1         3.2        90
2  A  2021-06-09 08:12:18.587    54    34    Morning  2021-06-09           14                  30.1         3.2        90
3  A  2021-06-09 08:12:18.716    56    53    Morning  2021-06-09           14                  30.1         3.2        90
4  A  2021-06-09 08:12:33.000    34    23    Morning  2021-06-09           14                  30.1         3.2        90

【讨论】:

    最近更新 更多