Reputation: 165
I have the following DF:
pd.DataFrame({'Data': ['Nov, 2018', '20 Sep 2019\xa0android-3','12 Nov 2019android-3', '11 Jun 2019roku-3\xa011 Sep 2019',
'11 Jun 2019roku-3\xa011 Sep 2019', '06 Jan 2020\xa0android-3', '19 Dec 2019\xa0android-3',
'12 Nov 2019\xa0apple-4', '22 Nov 2019\xa0apple-4', '11 Jul 2019\xa0x1-2']})
I am trying to create a second column that consists of only the platform in each row without the dates.
To do this, I have a function called extract_date()
:
def extract_date(date):
val = re.findall('\d{2} \w{3} \d{4}', date)
if len(val) == 1:
return val[0]
else:
return val
When I run this function on an individual string, I am able to get the result I want:
s = '27 Feb 2020 roku-5.002 Mar 2020 roku-5.0.1'
mydict = dict.fromkeys(extract_date(s), '')
for k, v in mydict.items():
s = s.replace(k, v).strip()
'roku-5.0 roku-5.0.1'
However, when I try to apply it to the Data column I don't get the same results:
def strip_dates(x):
if type(x) == float:
return x
else:
mydict = dict.fromkeys(extract_date(x), '')
for k, v in mydict.items():
return x.replace(k, v).strip()
df['Data Text'] = df.apply(lambda row: strip_dates(row['Data']), axis=1)
Data Data Text
0 Nov, 2018 None
1 20 Sep 2019 android-3 0 Sep 019 android-3
2 12 Nov 2019android-3 2 Nov 209android-3
3 11 Jun 2019roku-3 11 Sep 2019 roku-3 11 Sep 2019
4 11 Jun 2019roku-3 11 Sep 2019 roku-3 11 Sep 2019
Can anybody tell me what is wrong with my approach in applying the function? Thanks.
Upvotes: 0
Views: 562
Reputation: 16660
In your function:
def strip_dates(x):
if type(x) == float:
return x
else:
mydict = dict.fromkeys(extract_date(x), '')
for k, v in mydict.items():
return x.replace(k, v).strip()
You immediately return in the first loop over the items of mydict
dictionary:
return x.replace(k, v).strip()
Change it to:
def strip_dates(x):
if type(x) == float:
return x
else:
mydict = dict.fromkeys(extract_date(x), '')
s = str(x)
for k, v in mydict.items():
s = s.replace(k, v).strip()
return s
As you can see I reused the line from your function which you changed and hence s = str(x)
.
Upvotes: 1