我正在尝试从username列中提取子字符串。但我没有得到我的实际结果。 我的df如下所示
data = {'Name':['inf.negem.netmgmt', 'infbe_cdb', 'inf_igh', 'INF_EONLOG','inf.dkprime.netmgmt','infaus_mgo','infau_abr']}
df = pd.DataFrame(data)
print(df)
Name
0 inf.negem.netmgmt
1 infbe_cdb
2 inf_igh
3 INF_EONLOG
4 inf.dkprime.netmgmt
5 infaus_mgo
6 infau_abr
I tried following code.but i am not
df['Country'] = df['Name'].str.slice(3,6)
I would like to see output like below
output = {'Country':['No_Country', 'be', 'No_Country', 'No_Country','No_Country','aus','au']}
df = pd.DataFrame(output)
print(df)
Country
0 No_Country
1 be
2 No_Country
3 No_Country
4 No_Country
5 aus
6 au
Note: I would like to extract words between 'inf' and '_' as country and would like to create new column as Country. if nothing is there after inf then it's value is 'No_Country'
使用列表理解和
re.findall
:这里有一种使用^{} 的方法:
相关问题 更多 >
编程相关推荐