Reputation: 2747
I am trying to match a string(column) in csv files in python using Python but it does not match anything. I want the string to be match to be case insensitive. I am quite new but this is what I tried to do
test = pd.read_csv("data.csv")
mytest= pd.DataFrame(test, columns=[re.search("[a-zA-Z1-9_]", "columnname1", re.IGNORECASE),])
print(mytest)
Any help will be highly appreciated
Upvotes: 8
Views: 12603
Reputation: 394061
If I understand what you're after you can filter
your df to only return the columns where the name matches and make it case-insensitive:
In [298]:
df = pd.DataFrame({'columnname1':np.arange(5), 'ColumnName1':np.arange(5), 'columnname2':0, 'column name 1':0})
df
Out[298]:
ColumnName1 column name 1 columnname1 columnname2
0 0 0 0 0
1 1 0 1 0
2 2 0 2 0
3 3 0 3 0
4 4 0 4 0
In [299]:
import re
df.filter(regex=re.compile("columnname1", re.IGNORECASE))
Out[299]:
ColumnName1 columnname1
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
EDIT
For matching just the name without words preceding it, so matching on 'Test' but not 'My Test':
In [52]:
df = pd.DataFrame({'Test':np.arange(5), 'ColumnName1':np.arange(5), 'My Test':0, 'My column name 1':0})
import re
df.filter(regex=re.compile(r"^Test$", re.IGNORECASE))
Out[52]:
Test
0 0
1 1
2 2
3 3
4 4
So the ^
looks for 'Test' at the beginning of the str and the $
marks the end of the pattern to search, there is a handy cheat sheet.
Upvotes: 10