我有一个链接列表,例如:
Website
www.uk_nation.co.uk
www.nation_ny.com
www.unitednation.com
www.nation.of.freedom.es
www.freedom.org
等等
以上是我的数据表中的列的样子。正如您所看到的,它们有一个共同的单词"nation
"。我想对它们进行标记/分组,并在数据帧中添加一列,以使用布尔值(True/false;例如列:Nation?
选项:True/False
(进行响应。
Website Nation?
www.uk_nation.co.uk True
www.nation_ny.com True
www.unitednation.com True
www.nation.of.freedom.es True
www.freedom.org False
我需要这样做,以便以一种更容易(也可能更快(的方式对网站进行分类。你对如何做有什么建议吗?
欢迎任何帮助。
尝试str.contains
df['Nation']=df.Website.str.upper().str.contains('NATION')
0 True
1 True
2 True
3 True
4 False
Name: Website, dtype: bool
这是我的建议:
import pandas as pd
df = pd.DataFrame({'Website': ['www.uk_nation.co.uk',
'www.nation_ny.com',
'www.unitednation.com',
'www.nation.of.freedom.es',
'www.freedom.org']})
df['Nation?'] = df['Website'].str.contains("nation")
print(df)
输出:
Website Nation?
0 www.uk_nation.co.uk True
1 www.nation_ny.com True
2 www.unitednation.com True
3 www.nation.of.freedom.es True
4 www.freedom.org False
这应该做到:
df['Nation?']= df['website'].apply(lambda x: 'nation' in x.lower())