0

I have a dataframe filled with twitter data. The columns are:

  • row_id : Int
  • content : String
  • mentions : [String]
  • value : Int

So for every tweet I have it's row id in the dataframe, the content of the tweet, the mentions used in it (for example: '@foo') as an array of strings and a value that I calculated based on the content of the tweet.

An example of a row would be:

  • row_id : 12
  • content : 'Game of Thrones was awful'
  • mentions : ['@hbo', '@tv', '@dissapointment', '@whatever']
  • value: -0.71

So what I need is a way to do the following 3 things:

  • find all rows that contain the mention '@foo' in the mentions-field
  • find all rows that ONLY contain the mention '@foo' in the mentions-field
  • above two but checking for an array of strings instead of checking for only one handle

If anyone could help met with this, or even just point me in the right direction that'd be great.

2

1 Answer 1

1

Let's call your DataFrame df.

For the first task you use:

result = df[(Dataframe(df['mentions'].tolist()) == '@foo').any(1)]

Here, the Dataframe(df['mentions']) creates a new DataFrame where each column is a mention and each row a tweet.

Then == '@foo' generates a boolean dataframe containing True where the mentions are '@foo'.

Finally .any(1) returns a boolean index which elements are True if any element in the row is True.

I think with this help you can manage to solve the rest for yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

This returns to an empty dataframe. I also tried checking what Dataframe(df['mentions']) looks like, but it's just a 1-column dataframe with only the one 'mentions' column containing the array of mentions for each tweet. I'd post a screenshot of my output to better explain it, but I don't know how to do that in a comment :v
Sorry, you should make first df['mentions'].tolist().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.