This question already has answers here:
How can I check for a new line in string in Python 3.x?
(4 answers)
Closed 1 year ago.
Looking for ways in which I can run an equivalent of a 'find' in Python in order to be able to identify line breaks.
I have tried using this but it didn't return any results unexpectedly:
df[df.isin(['\n']).any(axis=1)]
The str accessor has a function to search for substrings.
df["colA"].str.contains(r"\n")
Use it in conjunction with apply to get your solution.
df.apply(lambda s: s.str.contains(r"\n"))
If you want a pd.DataFrame as result, use:
df1 = testdf[testdf['B'].str.contains('\n')]
Another solution would be with iloc and np.where:
testdf.iloc[np.where(testdf['B'].str.contains('\n', regex=False))]
Related
This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed last month.
//func_to_exec parameter is coming from database dynamically.
func_to_exec='split("\|")[0].split(",")[1]'
pl='mancity,manunited,arsenal|2|3|4|5'
is there anyway to call
pl.func_to_exec
I saw exec and eval functions are only for integers. I cant find any solution for strings.
Thx for suggestions.
You can use the exec function for that:
pl = 'mancity,manunited,arsenal|2|3|4|5'
func_to_exec = 'split("\|")[0].split(",")[1]'
exec(f'result = pl.{func_to_exec}')
print(result) # Output: 'manunited'
This question already has answers here:
Python: find string in file
(2 answers)
Closed 2 months ago.
By using python I need to know how to find a substring in a text file.
I tried using in and not in function in python to find a substring from a text file but i am not clear about it
Finding the index of the string in the text file using readline() In this method, we are using the readline() function, and checking with the find() function, this method returns -1 if the value is not found and if found it returns 0.
finding the index of the string in the text file using readline()In this method,we are using the readline()function,and checking with the find()function,this method returns-1 if the values is not found and if found it returns o
This question already has answers here:
pandas select from Dataframe using startswith
(5 answers)
Closed 3 years ago.
It seems like straight forward thing however could not find appropriate SO answer.
I have a column called title which contain strings. I want to find out rows that starts with letter "CU".
I've tried using df.loc however It's giving me indexError,
Using regex, re.findall(r'^CU', string)
returns 'CU' instead of full name ex: 'CU abcd'. How can I get full name that starts with 'CU'?
EDIT: SORRY, I did not notice it was a duplicate question, problem solved by reading duplicate question.
You can try:
string.startswith("CU")
This question already has answers here:
Extract part of a regex match
(11 answers)
Closed 3 years ago.
match_next = re.search(r'(再来週)の(.曜日)', '再来週の月曜日')
when I run match_next.group[1], I got the following:
TypeError: 'builtin_function_or_method' object is not subscriptable
Even if the match fails, why does the group function report this error?
The docs have the properties and how to use them. Basically the group method of the Match Object should be called with 1 or more integers indicating which groups you want to access.
match_next = re.search(r'(再来週)の(.曜日)', '再来週の月曜日')
match_next.group(1)
This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 5 years ago.
I want to strip the substring '_pf' from a list of strings. It is working for most of them, but not where there is a p in the part of the string I want to remain. e.g.
In: x = 'tcp_pf'
In: x.strip('_pf')
Out:
'tc'
I would expect the sequence above to give an output of 'tcp'
Why doesn't it? Have i misunderstood the strip function?
you can use:
x = 'tcp_ip'
x.split('_ip')[0]
Output:
'tcp'
You can also use spilt function like below,
x.split('_pf')[0]
It will give you tcp.