Reputation: 1631
I am trying to take the results I get from regex, i.e.
['11J']
['4C']
['5,']
[]
['04 ', '05 ', '48T']
And store those values in a new column (i.e. Apt) of an existing pandas data frame.
Sample data (Excel file)
index id apt address job description
0 122092476 207 EAST 74 STREET blah blah 11J blah
1 122096043 2092 8TH AVENUE blah 4C blah blah
Code
import pandas as pd
import re
df = pd.read_excel('/Users/abc/Desktop/Apartment.xlsx', sheetname=0)
df['Apt'] = 'None'
top5 = df.head()
t5jobs = top5['Job Description']
d = []
for index, job in enumerate(t5jobs):
result = re.findall(r'\d\d\D', job) or re.findall(r'\d\D', job) or re.findall(r'PH\D', job)
#print(str(result))
d.append(str(result))
df2 = pd.DataFrame([[d]], columns=list('Apt'))
df.append(df2)
I am getting this error:
AssertionError: 3 columns passed, passed data had 1 columns
How can I get these values inserted in the Apt column (overwrite None)?
Desired Output:
index id apt address job description apt
0 122092476 207 EAST 74 STREET blah blah 11J blah 11J
1 122096043 2092 8TH AVENUE blah 4C blah blah 4C
Upvotes: 2
Views: 2463
Reputation: 210982
try this (for pandas 0.18.0+):
In [11]: df['Apt'] = df['job description'].str.extract(r'\b(\d{1,2}\D)\b', expand=True)
In [12]: df
Out[12]:
id apt address job description Apt
index
0 122092476 207 EAST 74 STREET blah blah 11J blah 11J
1 122096043 2092 8TH AVENUE blah 4C blah blah 4C
for pandas versions < 0.18.0:
df['Apt'] = df['job description'].str.extract(r'\b(\d{1,2}\D)\b')
Upvotes: 2