0

I am preforming a filter on columns containing tweet_volume using regex, by the following command:

tweet_volume_df = df_merged.filter(regex=("tweet_volume.*"))

Within df_merged there is a column for name. I would like to include columns name in tweet_volume_df.

How do I perform this operation in a single line?

2 Answers 2

2

You can do with

df.filter(regex=r'(Name|tweet_volume.*)',axis=1)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use "|" in the regular expression, to match name or tweet_volume.*:

import pandas as pd

data = [list(range(5))]
headers = ['name', 'tweet_volume1', 'tweet_volume2', 'a', 'b']
df_merged = pd.DataFrame(data, columns=headers)

tweet_volume_df = df_merged.filter(regex=("tweet_volume.*|name"))

Comments

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.