【问题标题】:how to use apply function/for loop on dataframe column content in python如何在python中的数据框列内容上使用应用函数/for循环
【发布时间】:2018-11-30 03:26:34
【问题描述】:

对于上下文,我正在查看数据科学家职位和职位描述的数据集,并试图确定这些职位描述中引用了每个学位级别的数量。

我能够让代码处理一个特定的职位描述,但现在我需要执行一个“for 循环”或等效于遍历“描述列”并累计计算每个教育级别的次数被引用。

sentence = set(data_scientist_filtered.description.iloc[30].split())
degree_level = {'level_1':{'bachelors','bachelor','ba'},
    'level_2':{'masters','ms','m.s',"master's",'master of science'},
    'level_3':{'phd','p.h.d'}}
results = {}
for key, words in degree_level.items():
    results[key] = len(words.intersection(sentence))
results

示例字符串是这样的: data_scientist_filtered.description.iloc[30]=

 'the team: the data science team is a newly formed applied research team within s&p global ratings that will be responsible for building and executing a bold vision around using machine learning, natural language processing, data science, knowledge engineering, and human computer interfaces for augmenting various business processes.\n\nthe impact: this role will have a significant impact on the success of our data science projects ranging from choosing which projects should be undertaken, to delivering highest quality solution, ultimately enabling our business processes and products with ai and data science solutions.\n\nwhat’s in it for you: this is a high visibility team with an opportunity to make a very meaningful impact on the future direction of the company. you will work with senior leaders in the organization to help define, build, and transform our business. you will work closely with other senior scientists to create state of the art augmented intelligence, data science and machine learning solutions.\n\nresponsibilities: as a data scientist you will be responsible for building ai and data science models. you will need to rapidly prototype various algorithmic implementations and test their efficacy using appropriate experimental design and hypothesis validation.\n\nbasic qualifications: bs in computer science, computational linguistics, artificial intelligence, statistics, or related field with 5+ years of relevant industry experience.\n\npreferred qualifications:\nms in computer science, statistics, computational linguistics, artificial intelligence or related field with 3+ years of relevant industry experience.\nexperience with financial data sets, or s&p’s credit ratings process is highly preferred.

示例数据框:

 position       company       description             location
data scientist  Xpert Staffing  this job is for..      Atlanta, GA
data scientist  Cotiviti     great opportunity of..   Atlanta, GA

【问题讨论】:

  • 您的代码没有正确的缩进
  • 复制错误,已修复。

标签: python pandas dataframe data-science


【解决方案1】:

我建议在这里使用isin() 方法,然后得到总和。

data = [['John',"ba"],['Harry',"ms"],['Bill',"phd"],['Mary', 'bachelors']]
df = pd.DataFrame(data,columns=['name','description'])

degree_level = {
    'level_1':{'bachelors','bachelor','ba'},
    'level_2':{'masters','ms','m.s',"master's",'master of science'},
    'level_3':{'phd','p.h.d'}
}

results = {}
for level, values in degree_level:
    results[level] = data_scientist_filtered['description'].isin(values).sum()

print(results)
#{"level_1": 2, "level_2": 1, "level_3": 1}

编辑 for 循环可以替换为推导式,仅供参考。

def num_of_degrees(degrees):
    return data_scientist_filtered['description'].isin(values).sum()

results = {level: num_of_degrees(values) for level, values in degree_level}

编辑 2

你展示了 df 的样子,现在我明白了问题所在。 你需要filter() df 然后得到count()

#just cleaning some unnessecary values from degrees_level
degree_level = {
'level_1':{'bachelor',' ba '},
'level_2':{'masters',' ms ',' m.s ',"master's"},
'level_3':{'phd','p.h.d'}}

results = {}

for level, values in degree_level:
    results[level] = df.query(' or '.join((f"column_name.str.contains({value})" for value in values)), case=False, engine='python').count()

类似的东西应该可以工作

【讨论】:

  • 我收到一个错误:要解压的值太多,描述列有 5000 个条目。每个条目是一个大概 50-200 个单词的字符串。不确定这是否会导致此类错误。
  • 更正答案,需要使用 sum() 而不是 count()。另外,我对iloc 不太熟悉,但如果您的 df 是这样填充的,可能仍需要包含它。
  • 还是不行。我添加了一个示例字符串以供参考。
  • 所以我猜.. 这个度数存储在 df 的什么位置?
  • 理想情况下,我想替换 iloc 中的 30 并放置一个类似“i”的索引,并像 C 中的 for 循环/while 循环,而 i
【解决方案2】:
The simple way to do this breakup of text is by using n gram compare of text column by column. 
Create a list of position, company, location for possible values to be found.
Later compare the list column by column and save it in a data frame which can be combined lastly.

text1 = "Growing company located in the Atlanta, GA area is currently looking to add a Data Scientist to their team. The Data Scientist will analyze business level data to produce actionable insights utilizing analytics tools"

text2 = "Data scientist data analyst"

bigrams1 = ngrams(text1.lower().split(), n)  # For description 
bigrams2 = ngrams(text2.lower().split(), n)  # For position dictionary 

def compare(bigrams1, bigrams2):
    common=[]
    for grams in bigrams2:
       if grams in bigrams1:
         common.append(grams)
    return common

compare(bigrams1, bigrams2)

Output as 
compare(trigrams1,trigrams2)
Out[140]: [('data', 'scientist')]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2019-05-27
    • 2014-06-19
    • 2021-06-13
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多