- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用数据框,并且必须进行分组才能对我的数据进行一些操作。
这是我的数据框的示例:
I SI deltas
1 10 0.1
1 14 0.1
2 10 0.1
2 18 0.3
1 17 0.05
2 30 0.3
1 10 0.4
1 14 0.2
2 10 0.1
2 18 0.2
1 17 0.15
现在,对于每个 I,我以这种方式计算 SI 的相对频率:
results = df.groupby(['I', 'SI'])[['deltas']].sum()
#for each I, we sum all the weights (Deltas)
denom = results.groupby('I')['deltas'].sum()
#for each I, we divide each deltas by the sum, getting them normalized to one
results.deltas = results.deltas / denom
所以我的数据框现在看起来像这样:
我=1
deltas
SI = 10 0.5
SI = 14 0.3
SI = 17 0.2
我=2
deltas
SI = 10 0.2
SI = 18 0.5
SI = 30 0.3
...
我需要做的是打印每个 I 的增量总和乘以它们的相对 SI:
I = 1 sum = 0.5 * 10 + 0.3*14 + 0.2*17 = 12.6
I = 2 sum = 0.2*10 + 18*0.5 + 30*0.3 = 21
但由于现在我正在使用索引为 I 和 SI 的数据框,我不知道如何使用它们。我尝试了这段代码:
for idx2, j in enumerate(results.index.get_level_values(0).unique()):
#print results.loc[j]
f.write("%d\t"%(j)+results.loc[j].to_string(index=False)+'\n')
但我不确定应该如何继续获取索引值
最佳答案
假设您在初始转换后有一个输入数据帧 df
。如果 SI
是您的索引,请通过 df = df.reset_index()
将其提升为列作为初始步骤。
I SI weight
0 1 10 0.5
1 1 14 0.3
2 1 17 0.2
3 2 10 0.2
4 2 18 0.5
5 2 30 0.3
然后您可以计算SI
和weight
的乘积,然后使用GroupBy
+ sum
:
res = df.assign(prod=df['SI']*df['weight'])\
.groupby('I')['prod'].sum().reset_index()
print(res)
I prod
0 1 12.6
1 2 20.0
对于孤立的单个数据帧,您可以使用 np.dot
为点积。
s = pd.Series([0.5, 0.3, 0.2], index=[10, 14, 17])
s.index.name = 'SI'
res = np.dot(s.index, s) # 12.6
关于python - pandas 列与其索引 groupby 的乘积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52273761/
我希望我的问题有一个非常简单的解决方案。我只是找不到它: 假设您有两个向量(一个是列向量,一个是行向量)A、B: A = [1,2,3] B = [4;5;6] 如果我们按如下方式将它们相乘,我们会得
我有一个 Tuple 的列表: "dog", 25 "cat", 5 "cat", 7 "rat", 4 "dog", 10 我需要的 Linq 查询规则必须满足以下条件:我需要按字符串值对元组进行分
给定 2 个不同的 NDarray,A 和 B,形状相同但尺寸任意,我如何获得 NDarray C,其中 C 是 A 和 B(含)范围内所有整数的乘积。 我的意思是A是起始数组,B是结束数组,我想要数
假设我需要为某些输入构建一个真值表,它要求我提供逻辑和、算术和和逻辑乘积。它们之间有什么区别? 最佳答案 逻辑和 - 一种计算机加法,当一个或两个输入变量为 1 时,结果为 1;当输入变量均为 0 时
我正在尝试执行一个简单的矩阵乘法 vector 乘法,但出于某种原因,我在几次乘法的结果中得到了错误的符号。我不知道为什么会这样,任何指针将不胜感激。 这是我的全部代码,即矩阵 * vector 函数
我在上一个主题中找到了一些关于 cuda 矩阵 vector 积的代码: Matrix-vector multiplication in CUDA: benchmarking & performanc
我遇到的第一个问题是显示三个数字中的最小和最大。出现两个单独的警报 - 第一个警报说第二大数字是最大的(因为它还没有考虑第三个数字),第二个警报正确地指出三个中最大的数字是最大的.不确定为什么会这样—
我有两个矩阵 a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) 我想得到元素乘积,[[1*5,2*6], [3*7,4*8]],等
我有一个数组和一个 vector : ArrayXd m1(3, 1337); ArrayXd v1(1, 1337); ArrayXd result(3, 1337); 现在我想将 m1 的每一行与
我有两个 3D 矩阵: a = np.random.normal(size=[3,2,5]) b = np.random.normal(size=[5,2,3]) 我想要每个切片分别沿 2 轴和 0
我正在创建一个 C++ 软件,我需要一个包装器,它基于 Eigen 库,实现类似于官方网页中解释的运算符* https://eigen.tuxfamily.org/dox/group__Matrixf
我正在尝试将张量 (m, n, o) 分解为矩阵 A(m, r)、B (n, r) 和 C (k, r)。这被称为 PARAFAC 分解。 Tensorly已经做了这种分解。 一个重要的步骤是将 A、
我目前正面临这个问题。我有两个矩阵 MatrixXf答: 0.5 0.5 0.5 0.50.694496 0.548501 0.680067 0.7171110
我有以下 df: df = pd.DataFrame({'A': ['foo', 'bar', 'dex', 'tru'], 'B': ['abc', 'def'
假设我们有 2 个 2X2 numpy 数组: X=np.array([[0,1],[1,0]]) 和 I=np.array([[1,0],[0,1]]) 考虑一下克罗内克产品 XX=X^X 我让符号
我想弄清楚这是 Eigen 中的错误还是我做错了什么。我只想要两个复数 vector [1,i] 和 [1,-i] 的点积。答案是 1*1 + i*(-i) = 2。但是 Eigen 给出的答案是零。
我的 C 代码有问题。我所做的就是这样: #include int main() { float zahlen[2]; for (int i = 0; i < 2; i++) {
为了找到数字的因数,我正在使用函数 void primeFactors(int n) # include # include # include # include using namespa
我是一名优秀的程序员,十分优秀!