繁体   English   中英

根据熊猫索引将熊猫列从数据框合并到另一个数据框

[英]Merging pandas column from dataframe to another dataframe based on their indices

我有一个数据帧df_one ,看起来像这样,其中video_id是索引:

+----------+--------------+---------------+--------------+----------------+---------------+------------------+
|          | video_length | feed_position | time_watched | unique_watched | count_watched | avg_time_watched |
+----------+--------------+---------------+--------------+----------------+---------------+------------------+
| video_id |              |               |              |                |               |                  |
| 5        |           17 | 12.000000     |           17 |              1 |             1 | 1.000000         |
| 10       |           22 | 10.000000     |            1 |              1 |             1 | 0.045455         |
| 15       |           22 | 13.000000     |           22 |              1 |             1 | 1.000000         |
| 22       |           29 | 20.000000     |            5 |              1 |             1 | 0.172414         |
+----------+--------------+---------------+--------------+----------------+---------------+------------------+

我还有另一个数据df_two ,看起来像这样,其中video_id也是索引:

+----------+--------------+---------------+--------------+----------------+------------------------+
|          | video_length | feed_position | time_watched | unique_watched | count_watched_yeterday |
+----------+--------------+---------------+--------------+----------------+------------------------+
| video_id |              |               |              |                |                        |
| 5        |          102 | 11.333333     |           73 |              6 |                      6 |
| 15       |           22 | 13.000000     |           22 |              1 |                      1 |
| 16       |           44 | 2.000000      |           15 |              1 |                      1 |
| 17       |          180 | 23.333333     |           53 |              6 |                      6 |
| 18       |           40 | 1.000000      |           40 |              1 |                      1 |
+----------+--------------+---------------+--------------+----------------+------------------------+

我想要做的就是合并count_watched_yeterday从列df_twodf_one根据每个指标。

我试过了:

video_base = pd.merge(df_one, df_two['count_watched_yeterday'], how='left', on=[df_one.index, df_two.index])

但是我遇到了这个错误: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

实际上,我认为最简单的方法是直接分配:

In [13]:    
df['count_watched_yesterday'] = df1['count_watched_yeterday']
df['count_watched_yesterday']

Out[13]:
video_id
5      6
10   NaN
15     1
22   NaN
Name: count_watched_yesterday, dtype: float64

之所以有效,是因为它将与索引值对齐,在索引值没有匹配值的情况下,会将NaN分配为该值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM