Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 操作一列中的字符串,并将值附加到DataFrame-Pandas中的新行_String_Python 3.x_Pandas - Fatal编程技术网

String 操作一列中的字符串,并将值附加到DataFrame-Pandas中的新行

String 操作一列中的字符串,并将值附加到DataFrame-Pandas中的新行,string,python-3.x,pandas,String,Python 3.x,Pandas,我有以下格式的熊猫数据帧中的数据: 活动|消费| L003佛罗里达巴拿马| 800| 加利福尼亚州L015、L020-兰乔600| 我需要的是将活动列中以大写字母“L”开头并以三位数字结尾的每个值附加到一个新列中。更复杂的是,如果在campaign列的一个字符串中有多个这种格式的值,那么我需要在其自己的新行中列出每个值 输出结果如下: 活动|消费|商店 L003佛罗里达巴拿马| 800 | L003 加利福尼亚州L015,L020-兰乔600,L015 加利福尼亚州L015,L020-兰乔600

我有以下格式的熊猫数据帧中的数据:

活动|消费|

L003佛罗里达巴拿马| 800|

加利福尼亚州L015、L020-兰乔600|

我需要的是将活动列中以大写字母“L”开头并以三位数字结尾的每个值附加到一个新列中。更复杂的是,如果在campaign列的一个字符串中有多个这种格式的值,那么我需要在其自己的新行中列出每个值

输出结果如下:

活动|消费|商店

L003佛罗里达巴拿马| 800 | L003

加利福尼亚州L015,L020-兰乔600,L015

加利福尼亚州L015,L020-兰乔600,L020


抱歉,如果这没有意义,请让我知道我是否可以澄清。

您可以使用
提取所有内容
然后合并到原始数据帧

 df.reset_index().merge(df.Campaign.str.extractall('(\\bL\\d{3})').reset_index(),
           left_on='index',right_on  = 'level_0').drop(['index','level_0','match'],
           axis = 1).rename({0:'store'},axis = 1)

Out[65]: 
                 Campaign  Spend store
0         L003-FL-Panama     800  L003
1  L015, L020 CA- Rancho     600  L015
2  L015, L020 CA- Rancho     600  L020

您可以使用
extractall
然后合并到原始数据帧

 df.reset_index().merge(df.Campaign.str.extractall('(\\bL\\d{3})').reset_index(),
           left_on='index',right_on  = 'level_0').drop(['index','level_0','match'],
           axis = 1).rename({0:'store'},axis = 1)

Out[65]: 
                 Campaign  Spend store
0         L003-FL-Panama     800  L003
1  L015, L020 CA- Rancho     600  L015
2  L015, L020 CA- Rancho     600  L020

你能告诉我们你最近的尝试的代码吗,包括它吗?你能告诉我们你最近的尝试的代码吗,包括它吗?是的!非常感谢你!多谢各位