I have a pandas df
as the following.
Number | Word |
---|---|
1 1 1 | a b c |
2 2 | hi it's |
3 3 3 3 | hello world hi everyone |
4 | can't |
I want to detect whether there is an apostrophe in the word on the right, and add the same number that is on the left in the corresponding row 2 times.
i.e. The output should be like the following.
Number | Word |
---|---|
1 1 1 | a b c |
2 2 2 2 | hi it's |
Because there is an apostrophe in "it's" on the right in the second row, I want to add the same number twice on the left to the same cell.
I tried the following.
if df1['Word'].str.contains("'").any():
l.append(df1.iloc[0]['I'])
But I'm unsure about what to do after.
CodePudding user response:
don't think difficult, your solution is simple:
df1.apply(check_apostrophe, axis=1)
with pandas.DataFrame.apply method and set axis=1
you can apply a function to each row, my function is check_apostrophe
:
def check_apostrophe(x):
if "'" in str(x["Word"]):
x["Number"] = repeat(x["Number"])
return x
def repeat(string):
string = " " string[-1] #if your string is striped
return string
test: https://onecompiler.com/python/3xfve7kur
result:
Number | Word |
---|---|
1 1 1 | a b c |
2 2 2 | hi it's |
3 3 3 3 | hello world hi everyone |
4 4 | can't |
CodePudding user response:
You could write a conditional statement with np.where
to update the Number column accordingly.
import pandas as pd
import numpy as np
df = pd.DataFrame({'Number': ['1 1 1', '2 2', '3 3 3 3', '4'],
'Word': ['a b c', "hi it's", 'hello world hi everyone', "can't"]})
df['Number'] = np.where(df['Word'].str.contains("'"),
df['Number'] ' ' df.Number.str.split().str[0] ' ' df.Number.str.split().str[0],
df['Number'])
Output
Number Word
0 1 1 1 a b c
1 2 2 2 2 hi it's
2 3 3 3 3 hello world hi everyone
3 4 4 4 can't