- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个(非常大的)系列,其中包含关键字(例如,每行包含多个由“-”分隔的关键字
In[5]: word_series
Out[5]:
0 the-cat-is-pink
1 blue-sea
2 best-job-ever
dtype: object
我有另一个系列,其中包含每个单词的分数属性(单词是索引,分数是值),例如:
In[7]: all_scores
Out[7]:
the 0.34
cat 0.56
best 0.01
ever 0.77
is 0.12
pink 0.34
job 0.01
sea 0.87
blue 0.65
dtype: float64
我的 word_series 中的所有单词都出现在我的分数中。我正在尝试根据 all_scores 中每个单词的平均得分,找到为 word_series 的每一行分配分数的最快方法。如果一行是 n/a,则分数应该是分数的平均值。
我试过用这种方式应用,但是速度太慢了。
scores = word_series.apply(
lambda x: all_scores[x.split('-')].mean()).fillna(
all_scores.mean())
然后我想我可以使用 str.replace 将 all_words 拆分为列,并可能使用这个新矩阵 M 和我的单词执行矩阵乘法类型的运算,例如 M.mul(all_scores),其中 M 中的每一行都与基于以下值的值匹配all_scores 的索引。这将是第一步,得到平均值然后我可以除以每行中非 na 的数量
In[9]: all_words.str.split('-', expand=True)
Out[9]:
0 1 2 3
0 the cat is pink
1 blue sea None None
2 best job ever None
这样的操作可行吗?还是有另一种快速的方法来实现这一目标?
最佳答案
在 pandas 中处理字符串数据很慢,因此请使用 Series
和 mean
的 map 列表理解:
from statistics import mean
L = [mean(all_scores.get(y) for y in x.split('-')) for x in word_series]
a = pd.Series(L, index=word_series.index)
print (a)
0 0.340000
1 0.760000
2 0.263333
dtype: float64
或者:
def mean(a):
return sum(a) / len(a)
L = [mean([all_scores.get(y) for y in x.split('-')]) for x in word_series]
a = pd.Series(L, index=word_series.index)
如果可能,一些不匹配的值将参数 np.nan
添加到 get
并使用 numpy.nanmean
:
L = [np.nanmean([all_scores.get(y, np.nan) for y in x.split('-')]) for x in word_series]
a = pd.Series(L, index=word_series.index)
或者:
def mean(a):
return sum(a) / len(a)
L = [mean([all_scores.get(y, np.nan) for y in x.split('-') if y in all_scores.index])
for x in word_series]
关于python - pd.Serie 的每一行的平均 "score"基于其通过另一个分数 Series 映射的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54822308/
(请注意这里有一个问题 Pandas: group by and Pivot table difference ,但是这个问题是不同的。) 假设您从一个 DataFrame 开始 df = pd.Da
我在Windows 10上安装了Anaconda 3。我正在使用pd.read_csv()加载CSV文件,但收到错误消息。首先,我尝试了df=pd.read_csv(‘C:\Direct_market
我的输入数据是以下形式: gold,Program,MethodType,CallersT,CallersN,CallersU,CallersCallersT,CallersCallersN,
是否可以使用pd.merge复制以下内容 a = pd.DataFrame(dict(x=[1,2], y=[5,5])) b = pd.DataFrame(dict(x=[7,7], y=[12,1
我有一个像这样保存的数据框: Y_train_1.head() 0 4691.0 1 4661.0 2 4631.0 3 4601.0 4 4571.0 Y_train_
我有一个如下所示的 Excel 文件: CompanyName High Priority QualityIssue Customer1 Yes
题 为什么排序使用 pd.Series.sort_index使用分类索引时似乎不起作用?如何使用字母/数字以外的其他排序顺序对多索引 pd.Series 的索引进行排序? 移动电源 设置代码 impo
tt = pd.DataFrame({'a':[1,2,None,3],'b':[None,3,4,5]}) bb=pd.DataFrame(pd.isnull(tt).astype(int), in
示例代码: import pandas as pd import numpy as np sample = pd.DataFrame({"a":[1,2,3,1,2,3,1,2,3], "b":np.
我有一个 Pandas 系列和一个 Pandas 多索引数据框。 下面是一个简单的例子: iterables = [['milk', 'honey', 'dates'], ['jan', 'feb',
我拥有的: pd.Timestamp('2021-07-05 08:10:11') pd.Timestamp('2021-07-07 12:13:14') 我在找什么: [pd.Timestamp('
在使用 pandas 时,我遇到了创建新 data-Frame 的两种最常见的方法。使用pandas如下; 1. pandas.read_csv() Type: 2. pandas.DataFram
伙计们,我有一个Dataframe df= pd.DataFrame({'Point_ID':[1,2,3,1,2,1] , 'Shape_ID': [84,85,86,87,88,89],'LOL'
在 pandas datetimeindex 中,dayofweek和 weekday似乎是一样的。他们只是彼此的别名吗?我发现了这些功能 here 最佳答案 根据pandas源码定义的Datetim
我正在尝试按另一个按特定顺序排序的系列对 DataFrame (axis = 0) 进行排序。 例子:DataFrame 包含 CountryCodes 的索引:'AUS'、'BWA' ....(按字
我正在尝试使用 dask 读取 csv 文件,它给了我如下错误。但问题是我想要我的 ARTICLE_ID是 object(string) .谁能帮我成功读取数据? 回溯如下: ValueError:
为什么 pandas 有两个用于箱线图的函数:pandas.DataFrame.plot.box() 和 pandas.DataFrame.boxplot()? df = pd.DataFrame(n
我有一个多索引系列,如下所示。 > data = [['a', 'X', 'u', 1], ['a', 'X', 'v', 2], ['b', 'Y', 'u', 4], ['a', 'Z', 'u'
这个问题在这里已经有了答案: Inconsistency when setting figure size using pandas plot method (2 个答案) 关闭 4 年前。 在下面
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
我是一名优秀的程序员,十分优秀!