Python apply()对我的df的一部分起作用,但返回“;TypeError:浮点对象不可编辑”;总体上
目标:我有一个数据框,root_df,其中一列是一些用逗号分隔的单词(如“门、豹、礼炮”)命名的单词。然后我有另一个数据帧freq_df,其中有两列,WORD(string)和SCORE(float)。我要做的是创建一个聚合列,其中包含来自第二个数据帧freq_df的分数,该总分数基于第一个数据帧root_df中的列。让我给你举个例子。在freq_df中,带有“door”的行的分数为342388,“panther”一词根本不在数据框中,“sallet”的分数为9238.07。因此,根_df中的列为342388+9238.07,即351626.07 问题:我已经能够使用.apply()对我的数据子集执行此操作,但当我尝试对整个数据集执行此操作时,它返回“TypeError:“float”对象不可编辑”。我想这可能是因为它们在“拆分单词”列中是一个NaN,所以我将所有NaN替换为“”以查看这是否有帮助,并返回了一个新错误,“TypeError:(“不可损坏的类型:'列表','发生在索引拆分单词处”)”。我不明白为什么这会对我的数据的一个子集起作用,而不是对整个事情起作用,我认为所有系列都有相同的数据类型。有人能解释一下发生了什么吗?有没有方法返回返回返回错误的行?如果有任何帮助,我们将不胜感激 这是完整的代码,包括来自Wikipedia表的数据框,用于复制问题。如果他们对我的代码有任何疑问或问题,请告诉我Python apply()对我的df的一部分起作用,但返回“;TypeError:浮点对象不可编辑”;总体上,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,目标:我有一个数据框,root_df,其中一列是一些用逗号分隔的单词(如“门、豹、礼炮”)命名的单词。然后我有另一个数据帧freq_df,其中有两列,WORD(string)和SCORE(float)。我要做的是创建一个聚合列,其中包含来自第二个数据帧freq_df的分数,该总分数基于第一个数据帧root_df中的列。让我给你举个例子。在freq_df中,带有“door”的行的分数为342388,“panther”一词根本不在数据框中,“sallet”的分数为9238.07。因此,根_df中的列
import numpy as np
import pandas as pd
import urllib.request
def get_score(field):
words_list = []
for word in field:
words_list.append(word)
mask = freq_df['Word'].isin(words_list)
return freq_df.loc[mask, 'Count (per billion)'].sum()
#Root DataFrame
root_urls = [r"https://en.wikipedia.org/wiki/List_of_Greek_and_Latin_roots_in_English/A%E2%80%93G",
r"https://en.wikipedia.org/wiki/List_of_Greek_and_Latin_roots_in_English/H%E2%80%93O",
r"https://en.wikipedia.org/wiki/List_of_Greek_and_Latin_roots_in_English/P%E2%80%93Z"]
root_dfs = []
for url in root_urls:
dfs = pd.read_html(url, header=0)
for i, df in enumerate(dfs):
if df.shape[1] != 5:
print('Deleted below DataFrame(s):\n', dfs[i].head())
del dfs[i]
root_dfs.extend(dfs)
root_df = pd.concat(root_dfs, ignore_index=True)
root_df.replace(to_replace="\[.*?]", value="", regex=True, inplace=True)
#Frequency DataFrame
url = r"https://en.wiktionary.org/wiki/Wiktionary:Frequency_lists/PG/2006/04/1-10000"
freq_dfs = pd.read_html(url, header=0)
freq_df = pd.concat(freq_dfs)
#Succesful use of apply
test = root_df.head().copy()
a = pd.DataFrame(columns=test.columns)
a.loc[0] = ['Test', 'Test', 'Test', 'Test', 'door, panther, salute'] # Adding the exact example I gave above
test = pd.concat([test, a], ignore_index=True)
test['Split words'] = test['English examples'].str.split(', ')
test_score = test['Split words'].apply(get_score) # LINE IN QUESTION : SUCCESS
print("\nSuccesful test:\n\n", test_score)
#Unsuccesful use of apply
root_df['Split words'] = root_df['English examples'].str.split(', ')
score = root_df['Split words'].apply(get_score) # LINE IN QUESTION : FAIL
print(score)
我认为您不需要使用
apply
。您可以在一个长系列中获取英语示例中的所有单词,然后使用map
映射freq_df
中的值,然后汇总英语示例的每个原始列表
# First get the score mapping series
score = freq_df.set_index('Word')['Count (per billion)']
# use stack to make one long series of words from
# english examples
stacked_words = root_df['English examples'].str.split(',\s*', expand=True).stack()
# map all the english example words to their score
# and then sum up each group(original row)
stacked_words.map(score).groupby(level=0).sum().fillna(0)
0 56157.78
1 0.00
2 0.00
3 0.00
4 0.00
5 0.00
6 0.00
7 0.00
8 0.00
9 0.00
10 0.00
11 0.00
12 11422.40
13 190547.67
....
谢谢!我最初的想法是尝试使用replace之类的东西,但我想不出来,所以我不得不使用.apply()。只是为了确保我理解.map()基本上正确,它需要两个系列。它从第一个系列中获取值,并用第二个系列的值替换它们,其中Series1 value=Series2 index。对吗?