【问题标题】:TypeError: can't multiply sequence by non-int of type 'numpy.float64' - multiply column by valueTypeError:不能将序列乘以“numpy.float64”类型的非整数 - 将列乘以值
【发布时间】:2020-01-12 05:28:34
【问题描述】:

我在通过将现有列乘以一个值来在我的数据框中创建新列时遇到问题 - 我查看了类似的问题,但无法理解如何修复以下代码:

list = []

i = 1
for col in df.columns[1:19]:

    #calculations
    x = df[[df.columns[i], df.columns[i+1], df.columns[i+2]]].values
    Q = np.cov(x.T)

    eval, evec = np.linalg.eig(Q)

    w = np.array([2*(evec[0,2]/evec[1,2]),2*(evec[1,2]/evec[1,2]),2*(evec[2,2]/evec[1,2])])

    #create new columns in dataframe with applied weights
    df['w1_PCA'] = df.columns[i] * w[0]
    df['b_PCA'] = df.columns[i+1] * w[1]
    df['w2_PCA'] = df.columns[i+2] * w[2]

    i = i + 1

print(x)

收到错误如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-d7d86010b8f8> in <module>
 19 
 20     #create new columns in dataframe for back-applied PCA weights
---> 21     df['w1_PCA'] = df.columns[i] * w[0]
 22     df['b_PCA'] = df.columns[i+1] * w[1]
 23     df['w2_PCA'] = df.columns[i+2] * w[2]

TypeError: can't multiply sequence by non-int of type 'numpy.float64'

有人可以告诉我我在哪里做错了吗?

非常感谢任何帮助!

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    抛出错误是因为数据框 df 的列号 i 是字符串(在我的情况下是您的代码)或整数。您首先需要使用float() 将int 转换为float。 我为您的问题创建了一个简短示例,并且可以根据我的理解消除错误,同时添加另外三个插入了一些值的列。我希望您可以将此解决方案应用于您的数据框或数据集。您可以在下面找到两个示例,具体取决于您想要做什么。

    解决方案 1:

    import numpy as np
    import pandas as pd
    df = pd.DataFrame({'a': [1,2,3], 'b': [2,3,4], 'c': [2,3,4], 'd': [2,3,4], 'e': [2,3,4], 'f': [2,3,4], 'g': [2,3,4]})
    
    list = []
    
    i = 1
    for col in df.columns[1:5]:
    
        #calculations
        x = df[[df.columns[i], df.columns[i+1], df.columns[i+2]]].values
        Q = np.cov(x.T)
    
        eval, evec = np.linalg.eig(Q)
    
        w = np.array([2*(evec[0,2]/evec[1,2]),2*(evec[1,2]/evec[1,2]),2*(evec[2,2]/evec[1,2])])
    
        #create new columns in dataframe with applied weights
        df['w1_PCA'] = float(df['a'][0]) * w[0]
        df['b_PCA'] = float(df['b'][0]) * w[1]
        df['w2_PCA'] = df['c'][0] * w[2]
        i = i + 1
    

    在这种情况下生成的df 是:

        a   b   c   d   e   f   g   w1_PCA  b_PCA   w2_PCA
    0   1   2   2   2   2   2   2   -0.0    4.0     -4.0
    1   2   3   3   3   3   3   3   -0.0    4.0     -4.0
    2   3   4   4   4   4   4   4   -0.0    4.0     -4.0
    

    或者,您可以在 df['a'] 列上应用函数并将结果存储在新列中。您必须将代码的第 21 行到第 23 行更改为以下三行。 这是函数到整列的映射:

    解决方案 2

        df['w1_PCA'] = df['a'].apply(lambda x: float(x) * w[0])
        df['b_PCA'] = df['b'].apply(lambda x: float(x) * w[1])
        df['w2_PCA'] = df['c'].apply(lambda x: float(x) * w[2])
    

    结果:

        a   b   c   d   e   f   g   w1_PCA  b_PCA   w2_PCA
    0   1   2   2   2   2   2   2   -0.0    4.0     -4.0
    1   2   3   3   3   3   3   3   -0.0    6.0     -6.0
    2   3   4   4   4   4   4   4   -0.0    8.0     -8.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2021-08-07
      • 2010-11-15
      • 2010-12-30
      • 2012-10-09
      相关资源
      最近更新 更多