这里的新程序员,非常感谢这个知识渊博的社区愿意提供的任何帮助。你知道吗
我在一个pandas数据框中有一列140000个文本字符串(公司名称),在这个数据框中,我想去掉字符串中/周围的所有空格,删除所有标点符号,替换特定的子字符串,并统一转换为小写。然后我想获取字符串中的前0:10元素,并将它们存储在新的dataframe列中。你知道吗
这是一个可复制的例子。你知道吗
import string
import pandas as pd
data = ["West Georgia Co",
"W.B. Carell Clockmakers",
"Spine & Orthopedic LLC",
"LRHS Saint Jose's Grocery",
"Optitech@NYCityScape"]
df = pd.DataFrame(data, columns = ['co_name'])
def remove_punctuations(text):
for punctuation in string.punctuation:
text = text.replace(punctuation, '')
return text
# applying remove_punctuations function
df['co_name_transform'] = df['co_name'].apply(remove_punctuations)
# this next step replaces 'Saint' with 'st' to standardize,
# and I may want to make other substitutions but this is a common one.
df['co_name_transform'] = df.co_name_transform.str.replace('Saint', 'st')
# replace whitespace
df['co_name_transform'] = df.co_name_transform.str.replace(' ', '')
# make lowercase
df['co_name_transform'] = df.co_name_transform.str.lower()
# select first 0:10 of strings
df['co_name_transform'] = df.co_name_transform.str[0:10]
print(df)
co_name co_name_transform
0 West Georgia Co westgeorgi
1 W.B. Carell Clockmakers wbcarellcl
2 Spine & Orthopedic LLC spineortho
3 LRHS Saint Jose's Grocery lrhsstjose
4 Optitech@NYCityScape optitechny
我怎样才能把所有这些步骤都放到这样一个函数中呢?你知道吗
def clean_text(df[col]):
for co in co_name:
do_all_the_steps
return df[new_col]
谢谢
另一种解决方案,与前一种类似,但是在一个字典中有“to_replace”列表,因此您可以添加更多要替换的项。另外,前面的解决方案不会给出前10个。你知道吗
结果:
上一个解决方案(不显示前10个)
结果:
这样做不需要函数。试试下面的一行。你知道吗
最终输出为。你知道吗
您可以执行传递给
apply
方法的函数中的所有步骤:相关问题 更多 >
编程相关推荐