Reputation: 133
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:
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!
Upvotes: 0
Views: 500
Reputation: 1932
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])
Upvotes: 1
Reputation: 5052
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
Upvotes: 0