0

Is there a way to search for a string or substring in the column name and extract the entire column which name contains that particular string?

My data:

enter image description here

I want to search for "total" in the data frame and extract the entire column (the last column in this case)

Thank you in advance!

Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73
M_Arora
  • 113
  • 9

2 Answers2

1

If you are searching for keywords like Total, explicitly in column names, use the following:

For this dummy data:

Description,Qty,Unit Cost (AED), Total Cost (AED), Amount (xyz)
string 1, 3, 3000, 9000, 9500
string 1, 3, 3000, 9000, 9500
string 1, 3, 3000, 9000, 9500
string 1, 3, 3000, 9000, 9500
string 1, 3, 3000, 9000, 9500
string 1, 3, 3000, 9000, 9500

Try the following code:

import pandas as pd
import re

df = pd.read_csv('test.csv')
print(df)

col = [name for name in df.columns if len(re.findall(r'\b(?:total|amount)\b', name.lower()))!=0]

if len(col)!=0:
    print(df.loc[:, col])
anurag
  • 1,715
  • 1
  • 8
  • 28
0

You can try str.contains -

>>> total_mask = df['Description'].str.contains('total|Total')
>>> total_mask
0     True
1    False
2     True
Name: Description, dtype: bool

>>> df.loc[total_mask,:]
          Description  Total Cost (AED)
0  Compensation Total             100.0
2        Total Amount              20.0
Vaebhav
  • 4,672
  • 1
  • 13
  • 33