pandas 用元组乘级数

jvidinwx  于 2023-05-21  发布在  其他
关注(0)|答案(7)|浏览(123)

如何将序列与tuple相乘,序列中的每个值将乘以tuple中的每个值。
我写了一个代码的例子,向你展示预期结果的例子:

import numpy as np
import pandas as pd

def main():
    s = pd.Series(data=np.around(a=np.random.uniform(size=10), decimals=3))
    t = (2, 3, 5, 7)
    print(s)

if __name__ == '__main__':
    main()

我的输出:

0    0.709
1    0.567
2    0.894
3    0.660
4    0.348
5    0.465
6    0.974
7    0.832
8    0.888
9    0.265
dtype: float64

我需要得到这个结果:

0    (1.418, 2.127, 3.545, 4.963)
1    (1.134, 1.701, 2.835, 3.969)
2    (..., ..., ..., ...)
3    (..., ..., ..., ...)
4    (..., ..., ..., ...)
5    (..., ..., ..., ...)
6    (..., ..., ..., ...)
7    (..., ..., ..., ...)
8    (..., ..., ..., ...)
9    (..., ..., ..., ...)
dtype: float64
zf9nrax1

zf9nrax11#

试试看

import numpy as np
import pandas as pd

def main():
    s = pd.DataFrame(data=np.around(a=np.random.uniform(size=10), decimals=3))
    t = (2, 3, 5, 7)
    s[0] = s[0].apply(lambda x: tuple([(x *  y) for y in t]))
    print(s)

if __name__ == '__main__':
    main()

输出量

0           (1.168, 1.7519999999999998, 2.92, 4.088)
1           (1.332, 1.9980000000000002, 3.33, 4.662)
2                       (1.254, 1.881, 3.135, 4.389)
3  (1.372, 2.0580000000000003, 3.43, 4.8020000000...
4          (0.846, 1.269, 2.1149999999999998, 2.961)
5          (0.386, 0.579, 0.9650000000000001, 1.351)
6          (1.528, 2.292, 3.8200000000000003, 5.348)
7                            (0.64, 0.96, 1.6, 2.24)
8                        (1.75, 2.625, 4.375, 6.125)
9                       (1.234, 1.851, 3.085, 4.319)
dluptydi

dluptydi2#

您可以尝试:

>>> s.apply(lambda x: tuple(x*np.array(t)))

避免列表解析和循环

k3bvogb1

k3bvogb13#

s = pd.Series(data=np.around(a=np.random.uniform(size=10), decimals=3))
t = (2, 3, 5, 7)
s = pd.Series([i for i in zip(*[s*i for i in t])])
s

输出:

0    (1.162, 1.7429999999999999, 2.905, 4.067)
1    (1.746, 2.6189999999999998, 4.365, 6.111)
2                  (0.516, 0.774, 1.29, 1.806)
3                      (1.68, 2.52, 4.2, 5.88)
4                  (1.048, 1.572, 2.62, 3.668)
5                  (0.412, 0.618, 1.03, 1.442)
6                 (1.426, 2.139, 3.565, 4.991)
7    (1.162, 1.7429999999999999, 2.905, 4.067)
8    (0.652, 0.978, 1.6300000000000001, 2.282)
9                 (0.446, 0.669, 1.115, 1.561)
dtype: object
w46czmvw

w46czmvw4#

将元组转换为NumPy数组可以使您的工作更轻松。它只是将整个数组与Series中存在的元素相乘,然后u可以将NumPy数组再次转换为元组。虽然多次转换效率不高,但使用numpy数组处理更大的元组是有利的。如果元组不需要出现在系列中,处理NumPy数组是一个很好的实践。下面的代码可以完成这项工作。

import pandas as pd
    import numpy as np
    s = pd.Series(data=np.around(a=np.random.uniform(size=10), decimals=3))
    t = np.array((2, 3, 5, 7))
    s=pd.Series([tuple(x*t) for x in s]).
sy5wg1nm

sy5wg1nm5#

因为之前的答案都被接受了,我一个都不喜欢。我提出了我的解决方案,没有应用或循环:

import numpy as np
import pandas as pd

def main():
    s = pd.Series(data=np.around(a=np.random.uniform(size=10), decimals=3))
    print(s)
    t = (2, 3, 5, 7)
    print(t)
    r = s.to_frame('val') * pd.Series(t, index=['val'] * len(t))
    print(r)

if __name__ == '__main__':
    main()

乘法使用列的名称来匹配它们。使用相同的名称来解决问题。可能的输出:

0    0.179
 1    0.962
 2    0.755
 3    0.247
 4    0.378
 5    0.559
 6    0.757
 7    0.585
 8    0.481
 9    0.198
 dtype: float64
 (2, 3, 5, 7)
      val    val    val    val
 0  0.358  0.537  0.895  1.253
 1  1.924  2.886  4.810  6.734
 2  1.510  2.265  3.775  5.285
 3  0.494  0.741  1.235  1.729
 4  0.756  1.134  1.890  2.646
 5  1.118  1.677  2.795  3.913
 6  1.514  2.271  3.785  5.299
 7  1.170  1.755  2.925  4.095
 8  0.962  1.443  2.405  3.367
 9  0.396  0.594  0.990  1.386
0pizxfdo

0pizxfdo6#

以下是使用numpy广播的方法:

pd.Series(map(tuple,s.to_numpy()[:,None] * np.array(t)))

输出:

0            (0.598, 0.897, 1.4949999999999999, 2.093)
1             (0.588, 0.8819999999999999, 1.47, 2.058)
2                              (0.12, 0.18, 0.3, 0.42)
3                         (0.606, 0.909, 1.515, 2.121)
4                             (1.22, 1.83, 3.05, 4.27)
5    (1.584, 2.3760000000000003, 3.96, 5.5440000000...
6                             (0.86, 1.29, 2.15, 3.01)
7            (1.304, 1.956, 3.2600000000000002, 4.564)
8                             (0.06, 0.09, 0.15, 0.21)
9            (1.942, 2.913, 4.8549999999999995, 6.797)
xyhw6mcr

xyhw6mcr7#

您可能希望使用pandas DataFrame处理“二维”数据。您可以尝试以下操作:

import pandas as pd
import numpy as np

s = pd.Series(data=np.around(a=np.random.uniform(size=10), decimals=3))
t = (2, 3, 5, 7)

df = pd.DataFrame()

for i, value in enumerate(t):
    col = i
    df[col] = s * value

print(df)

此代码将打印:

0      1      2      3
0  1.640  2.460  4.100  5.740
1  1.544  2.316  3.860  5.404
2  1.556  2.334  3.890  5.446
3  0.710  1.065  1.775  2.485
4  1.816  2.724  4.540  6.356
5  0.778  1.167  1.945  2.723
6  0.628  0.942  1.570  2.198
7  0.800  1.200  2.000  2.800
8  0.538  0.807  1.345  1.883
9  1.520  2.280  3.800  5.320

相关问题