简体   繁体   English

如何去除Series中的空列表,同时将非空列表变为字符串形式?

[英]How to remove the empty list in the Series, and at the same time change the non-empty list into a string form?

The following is an example下面是一个例子

df=pd.DataFrame({'AAA','BBBB','CCC'},columns=['fruit'])
a=pd.Series({'0':'A','1':'B','2':'C'})
dict_a={}
for x in a:
dict_a[x]=''.join('%s' %a for a in filter(None,df['fruit'].str.findall(r'.*{}.*'.format(x))))

The dictionary values I get are in list form:['AAA'],['BBB'] but my ideal form is:'AAA','BBB' How to remove the empty list in the Series, and at the same time change the non-empty list into a string form,I want to use it as the value of the dictionary?我得到的字典值是列表形式:['AAA'],['BBB'] 但我理想的形式是:'AAA','BBB' 如何去掉Series中的空列表,同时改变将非空列表转换成字符串形式,我想用它作为字典的值吗?

To get only the 1st element of the match, take the list item at index 0:要仅获取匹配项的第一个元素,请获取索引 0 处的列表项:

for x in a:
    dict_a[x] = ''.join(m[0] for m in filter(None, df['fruit'].str.findall(r'.*{}.*'.format(x))))

# `dict_a` is:
{'A': 'AAA', 'B': 'BBBB', 'C': 'CCC'}

This will take only the first result of findall which might not give the results you want if you have more than one match in the dataframe or more than one match per row in the dataframe.如果您在 dataframe 中有多个匹配项或在 dataframe 中的每行有多个匹配项,则这将仅采用findall的第一个结果,这可能不会给出您想要的结果。

(Also, don't reuse the series name a in your loop. I've change it to m .) (另外,不要在循环中重复使用系列名称a 。我已将其更改为m 。)

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

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