Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stack argument to str.findall #4428

Closed
hayd opened this issue Aug 1, 2013 · 4 comments
Closed

Add stack argument to str.findall #4428

hayd opened this issue Aug 1, 2013 · 4 comments
Labels
Enhancement Strings String extension data type and string data

Comments

@hayd
Copy link
Contributor

hayd commented Aug 1, 2013

Some of the string methods return a Series of lists (e.g. findall) which isn't that convenient for doing more analysis:

df = pd.DataFrame([['@a @b'], ['@a'], ['@c']], columns=['tweets'])
at_mentions = df.tweets.str.findall('@[a-zA-Z0-9_]+')

In [3]: at_mentions
Out[3]:
0    [@a, @b]
1        [@a]
2        [@c]
Name: tweets, dtype: object

I think it would be nice to be able to stack the results:

In [4]: at_mentions.apply(pd.Series).stack()
Out[4]:
tweets
1       0    @a
        1    @b
2       0    @a
3       0    @c
dtype: object

see this SO answer

Could just do it directly like that, or perhaps make more efficient. Thoughts?

@jreback
Copy link
Contributor

jreback commented Aug 1, 2013

I actually like this (as the default)

In [7]: at_mentions.apply(lambda x: Series(1,x)).fillna(0)
Out[7]: 
   @a  @b  @c
0   1   1   0
1   1   0   0
2   0   0   1

@cpcloud
Copy link
Member

cpcloud commented Aug 1, 2013

clever....that's the sweet dummy variable hack

@jreback
Copy link
Contributor

jreback commented Sep 28, 2013

realted is #4685, pushing to 0.14

@jreback jreback modified the milestones: 0.15.0, 0.14.0 Mar 19, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 3, 2015
@datapythonista datapythonista modified the milestones: Contributions Welcome, Someday Jul 8, 2018
@mroeschke
Copy link
Member

I think explode solves this use case. Happy to reopen if this doesn't solve the original use case.

In [7]: at_mentions.explode()
Out[7]:
0    @a
0    @b
1    @a
2    @c
Name: tweets, dtype: object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Strings String extension data type and string data
Projects
None yet
Development

No branches or pull requests

5 participants