Reputation: 59
I am new to python, I have an issue with matching the names of the column of Dataframe in python. So, I have a string s = "8907*890a"
where a is the column name of a data frame. Now I want to match that with the column names of df which is there or not. I have tried it but the string is being taken as the whole. How to get only the 'a' from the whole string?
My code:
s = "8907*890a"
df=
a b c
0 rr 12 4
1 rt 45 9
2 ht 78 0
for col in df.columns:
for i in s.split():
print(i)
Which gives:
"8907*890a"
Expected out:
a
Upvotes: 0
Views: 272
Reputation: 749
The split function accepts a delimiter as a parameter. By default the delimiter is a space. So when you try s.split()
the interpreter is looking for a space in the string which it doesn't find in this case. So it returns the whole string as the output. If you try s.split('*')
you will get
8907
890a
as output. In your case it appears that splitting the string is not the best option to extract the column name. I would go with extracting the last character instead. This can be done using s[-1:]
Upvotes: 1