Home > Mobile >  How can I create a new column in a pandas data frame by extracting words from sentences in another c
How can I create a new column in a pandas data frame by extracting words from sentences in another c

Time:12-12

I have a pandas dataframe like this.

import pandas as pd
student_id = ['001', '002', '003', '004']
names = ['Jane', 'Mary', 'Andrew', 
'Paul']
address = ['7 karumu st Ikeja Lagos', '8 
logo street Umuahia Abia', 
       '10 jege close PH Rivers', '9 
Lekki gate Lagos']

test_1 = {'Student_ID': student_id, 
      'Name': names, 
      'Address': address}
df = pd.DataFrame(test_1)
df`

Output

and a list like this:

List = [Imo, Lagos, Abia, Ebonyi, Rivers]

So i am trying to iterate through the Address column and estract the states in the address which is also in the list. If a state in the list is spotted I would like to extract it and append to a new column called state.

I tried to use the iterrows() method but I am a bit lost

CodePudding user response:

You can filter like this:

df = df[df['Address'].str.contains('|'.join(List))]

CodePudding user response:

  • get the 'Adress' Column
  • convert to 'List' to DataFrame
  • After I think 'MERGE' you should use
  • Storage to last dafaFrame and add the as a another column

I think this will solve your problem

  • Related