简体   繁体   English

pandas | 根据条件从另一个 dataframe 获取数据

[英]pandas | get data from another dataframe based on condition

I have two dataframes:我有两个数据框:

products

+------------+--------------------+
| item_name  | item_tags          |
+------------+--------------------+
| blue_shirt | summer,winter,blue |
|            |                    |
+------------+--------------------+
| red_skirt  | spring,summer      |
+------------+--------------------+

and ordersorders

+------------+
| item       |
+------------+
| blue_shirt |
+------------+
| red_skirt  |
+------------+

and I want to create a new column in orders : when products.item_name == orders.item , I want to take the value of products.item_tags and add it to orders.我想在orders中创建一个新列:当products.item_name == orders.item时,我想获取products.item_tags的值并将其添加到订单中。

I've tried:我试过了:

orders['ItemTags'] = products.query("{0}=={1}".format(orders['item'], products['item_name']))['Tags']

But it gives me an error.但这给了我一个错误。

One way we can do this is with creating a dictionary from your products table, with your item_name column as your key and your item_tags column as your value , and then map it onto your orders item column:我们可以做到这一点的一种方法是从您的 products 表中创建一个字典,您的 item_name 列作为您的key ,您的 item_tags 列作为您的value ,然后将map放到您的 orders 项目列中:

products_dict = dict(zip(products.item_name,products.item_tags))
orders['item_tags'] = orders['item'].map(products_dict)

Output Output

orders
Out[83]: 
         item           item_tags
0  blue_shirt  summer,winter,blue
1   red_skirt       spring,summer

Another approach could be using of the merge function.另一种方法可能是使用merge function。

pd.merge(df_orders, df_products, left_on='item', right_on='item_name').drop(['item_name'], axis = 1)

By using merge method you can pass the both products and orders datasets and specify the column(s) for the join operations.通过使用merge方法,您可以传递productsorders数据集,并为join操作指定列。

import pandas as pd
df_products = pd.DataFrame(data={'item_name': ['blue_shirt', 'red_skirt'], 'item_tags': ['summer,winter,blue','spring,summer']})
df_orders = pd.DataFrame(data={'item': ['blue_shirt','red_skirt']})
df_orders = pd.merge(df_orders, df_products, left_on='item', right_on='item_name').drop(['item_name'], axis = 1)
print(df_orders)

Output Output

   item           item_tags
0  blue_shirt  summer,winter,blue
1  red_skirt       spring,summer

暂无
暂无

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

相关问题 更快地从一个数据帧获取行数据(基于条件)并合并到另一个b pandas python上 - Faster way to get row data (based on a condition) from one dataframe and merge onto another b pandas python Pandas 数据框根据另一列的条件创建新行 - Pandas dataframe create new rows based on condition from another column 熊猫根据其他数据框的条件对行进行划分 - Pandas divide rows based on condition from another dataframe Pandas:根据条件将值从一个 dataframe 合并到另一个 - Pandas: Merge values from one dataframe to another based on condition Pandas 根据来自另一个 dataframe 的计数和条件创建新列 - Pandas Create new column based on a count and a condition from another dataframe 根据另一列中的条件从 Pandas 数据框中提取值 - Extract Value From Pandas Dataframe Based On Condition in Another Column 熊猫:根据时间条件将行从一个数据框映射到另一个数据框 - Pandas: Map rows from one dataframe to another based on a time condition 根据条件从另一个 Dataframe 添加新数据到 Dataframe - Adding new data to a Dataframe from another Dataframe based on condition 根据来自另一个数据框的数据将值分配给Pandas数据框中的列 - Assign values to columns in Pandas Dataframe based on data from another dataframe Pandas数据框根据查询数据框中的值选择行,然后根据列值选择其他条件 - Pandas Dataframe Select rows based on values from a lookup dataframe and then another condition based on column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM