[英]How can I create a new column in a pandas data frame by extracting words from sentences in another column?
I have a pandas dataframe like this.我有一个这样的 pandas dataframe。
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`
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.
如果发现列表中的 state,我想将它和 append 提取到名为 state 的新列中。
I tried to use the iterrows() method but I am a bit lost我尝试使用 iterrows() 方法,但我有点迷路
You can filter like this:您可以像这样过滤:
df = df[df['Address'].str.contains('|'.join(List))]
I think this will solve your problem我想这会解决你的问题
Assuming that the state is always the last word in the address.假设 state 始终是地址中的最后一个字。
import numpy as np
states = ["Imo", "Lagos", "Abia", "Ebonyi", "Rivers"]
df["State"] = df["Address"].map(lambda x: state if (state:=x.split()[-1]) in states else np.nan)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.