首页 > 解决方案 > Python:使用数据框从字符串中提取子字符串

问题描述

试图从字符串中提取子字符串。为单个记录工作,但为多个记录获取 NAN。

预期输出:

1st Row:HKA-33711-15293045
2nd row:HKA-94612-14661285

代码尝试:

df1['o_position'] = df1['Event Value'].str.find('HKA-')
df1['o_position_to_18'] = df1['o_position']+18
df1["order_id"]=df1["Event Value"].str.slice(start=df1["o_position"],stop=df1['o_position_to_18'])
df1.head()

数据:

{'"af_currency"':'"INR"','"af_revenue"':429,'"af_customer_user_id"':'"33738413"','"af_price"':['"399"'],'"af_receipt_id"':'"HKA-33711-15293045"','"af_content_id"':['"72769"'],'"sessionId"':'"-1551484318545"','"af_quantity"':['"1"'],'"af_param_1"':'"HKA-33711-15293045"','"af_content_type"':'"order"'}

第二行:

{'"af_price"':['"2199"'],'"af_content_id"':['"72289"'],'"af_quantity"':['"1"'],'"af_param_1"':'"HKA-94612-14661285"','"af_content_type"':'"order"','"af_currency"':'"INR"','"af_revenue"':2199,'"af_receipt_id"':'"HKA-94612-14661285"'}

标签: pythonstringpandasdataframe

解决方案


推荐阅读