get columns containing string

Do you want to know the information regarding “get columns containing string”. If yes, you’re in the correct tutorial.

get columns containing string

import pandas as pd

data = 'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]
df = pd.DataFrame(data)

spike_cols = [col for col in df.columns if 'spike' in col]
print(list(df.columns))
print(spike_cols)
df2 = df.filter(regex='spike')
print(df2)

Final Thoughts

I hope this article helps you to know about “get columns containing string”. If you have any questions regarding this article please let us know via the comment section. Share this tutorial with your friends and family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment