简体   繁体   English

在 Python 中乘以 dataframe 中的数组

[英]Multiplying an array in a dataframe in Python

I am reading a specific column from a csv file.我正在阅读csv文件中的特定列。 I want to multiply the elements of this array with 2 .我想将此数组的元素与2相乘。 When I do so, the array size is doubling instead of multiplying each element with 2 .当我这样做时,数组大小加倍,而不是将每个元素与2相乘。 I have shared the CSV file.我已经分享了CSV文件。 I also present the current and expected outputs.我还介绍了当前和预期的输出。

import pandas as pd
data = pd.read_csv("Data.csv")

B = pd.read_csv("Data.csv", sep=',', usecols=['sigma'], squeeze=True)
print("B[0] =",B[0]*2)

The CSV file looks like CSV 文件看起来像

enter image description here在此处输入图像描述

The current output is当前的 output 是

B[0] = [array([[0.02109],
       [0.02109],
       [array([[0.02109],
       [0.02109]])]

The expected output is预期的 output 是

B[0] = [array([[.04218],
       [.04218]])]

Does this help?这有帮助吗?

df = pd.DataFrame({
    "col" : [[0.2, 0.3],[0.3, 0.4],[0.4, 0.5],[0.5, 0.6]]
})

Multiply it by 2乘以 2

df.col = df.col.apply(lambda x: [num * 2 for num in x])

Result结果

    col
0   [0.4, 0.6]
1   [0.6, 0.8]
2   [0.8, 1.0]
3   [1.0, 1.2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM