Python 基于索引数据框使用熊猫创建新的(更详细的)数据框
我为这个新手问题道歉,但我很难弄清楚熊猫的数据帧。我有一个数据框,上面有Python 基于索引数据框使用熊猫创建新的(更详细的)数据框,python,pandas,dataframe,Python,Pandas,Dataframe,我为这个新手问题道歉,但我很难弄清楚熊猫的数据帧。我有一个数据框,上面有 df_index: Product Title 100000 Sample main product 200000 Non-consecutive main sample 我有另一个数据框,其中包含更详细的产品列表,格式如下 df_details: Product Title 100000 Sample main produ
df_index:
Product Title
100000 Sample main product
200000 Non-consecutive main sample
我有另一个数据框,其中包含更详细的产品列表,格式如下
df_details:
Product Title
100000 Sample main product
100000-Format-English Sample product details
100000-Format-Spanish Sample product details
100000-Format-French Sample product details
110000 Another sample main product
110000-Format-English Another sample details
110000-Format-Spanish Another sample details
120000 Yet another sample main product
120000-Format-English Yet another sample details
120000-Format-Spanish Yet another sample details
...
200000 Non-consecutive main sample
200000-Format-English Non-consecutive sample details
200000-Format-Spanish Non-consecutive sample details
我想基于df_详细信息创建一个新的数据框,但只针对df_索引中显示的产品。理想情况下,它看起来像:
new_df:
Product Title
100000 Sample main product
100000-Format-English Sample product details
100000-Format-Spanish Sample product details
100000-Format-French Sample product details
200000 Non-consecutive main sample
200000-Format-English Non-consecutive sample details
200000-Format-Spanish Non-consecutive sample details
以下是我迄今为止所尝试的:
new_df = df_details[df_details['Product'][0:5] == df_index['Product'][0:5]]
这给了我一个错误:
ValueError: Can only compare identically-labeled Series objects
我也试过了
new_df = pd.merge(df_index, df_details,
left_on=['Product'[0:5]], right_index=True, how='left')
这确实给了我一个结果数据集,但不是我想要的类型,它不包括包含格式信息的细节行 您应该能够使用
.isin()
作为:
new_df = df_details[df_details['Product'].isin(df_index['Product']]
这将执行仅查找公共索引的掩码
编辑:这仅适用于列是否具有相同的字符串。要解决此问题,您可以使用str.contains()
和:
import re
# create a pattern to look for
pat ='|'.join(map(re.escape, df_index['Product']))
# Create the mask
new_df = df_details[df_details['Product'].str.contains(pat)]
如果将列格式化为字符串,则此操作有效。以下是我如何实现此操作的方法—我确信这并不漂亮,也不是实现此操作的最快方法,但它确实有效 我使用pandas的
.itetrow()
和一些for
和if
循环逐行遍历数据帧:
# create a list based on the 'Product' column of df_index
increment = 0
index_list = []
for product, row in df_index.iterrows():
prod_num = df_index.product.iloc[increment]
index_list.append(prod_num)
increment += 1
#construct a new data frame based on the rows in df_details that are found in index_list
new_df = pd.DataFrame(columns=detail_df.columns)
increment_detail = 0
for product, row in df_details.iterrows():
prod_num_detail = df_details.product.iloc[increment_detail]
prod_num_detail = prod_num_detail[0:6]
if str(prod_num_detail) in dupe_list:
new_df = new_df.append(df_details.iloc[increment_detail])
increment_detail += 1
else:
increment_detail += 1
美好的我只是在写同样的解决方案。这确实有点效果。不过,它没有给我200000格式的英文样式行。可能是因为它与看起来像200000的行不完全匹配?@nathan.hunt是的,你是对的,我认为所有行都有相同的格式…这将对“相同格式”行执行查找。。。考虑一个更普遍的解决方案。这应该会有帮助@Fabiolamana非常感谢你的帮助!我确实使用了
re.escape
,它很管用,但由于某种原因,我无法理解,它也给了我一些我没有预料到的行……而且在df_索引中找不到的行,我能说的很接近。不过,我确实找到了如何解决for
和if
与pandas的.iterrow()
循环的问题,所以我将发布这篇文章。