【问题标题】:How to multiply two created series to create third series如何将两个创建的系列相乘以创建第三个系列
【发布时间】:2018-08-27 06:18:19
【问题描述】:

我已经创建了两个系列,我想通过对前两个进行元素乘法来创建第三个系列。我的代码如下:

new_samples = 10 # Number of samples in series
a = pd.Series([list(map(lambda x:x,np.linspace(2,2,new_samples)))],index=['Current'])
b = pd.Series([list(map(lambda x:x,np.linspace(10,0,new_samples)))],index=['Voltage'])
c = pd.Series([x*y for x,y in zip(a.tolist(),b.tolist())],index=['Power'])

我的输出是:

TypeError: can't multiply sequence by non-int of type 'list'

为了清楚起见,我在下面粘贴了我的实际for 循环代码。我的数据框已经有三列Current,Voltage,Power。根据我的要求,我必须将新的值列表添加到现有列VoltageCurrent。但是,Power 值是通过乘以已经创建的值来创建的。我的代码如下:

for i,j in zip(IV_start_index,IV_start_index[1:]):            
    isc_act = module_allData_df['Current'].iloc[i:j-1].max()
    isc_indx = module_allData_df['Current'].iloc[i:j-1].idxmax()
    sample_count = int((j-i)/(module_allData_df['Voltage'].iloc[i]-module_allData_df['Voltage'].iloc[j-1]))
    new_samples = int(sample_count * (module_allData_df['Voltage'].iloc[isc_indx]))
    missing_current = pd.Series([list(map(lambda x:x,np.linspace(isc_act,isc_act,new_samples)))],index=['Current'])
    missing_voltage = pd.Series([list(map(lambda x:x,np.linspace(module_allData_df['Voltage'].iloc[isc_indx],0,new_samples)))],index=['Voltage'])
    print(missing_current.tolist()*missing_voltage.tolist())

示例数据:module_allData_df.head()

     Voltage  Current    Power  
0  33.009998   -0.004 -0.13204  
1  33.009998    0.005  0.16505  
2  32.970001    0.046  1.51662  
3  32.950001    0.087  2.86665  
4  32.919998    0.128  4.21376 

样本数据:module_allData_df.iloc[120:126] 你也需要这个

       Voltage  Current    Power  
120   0.980000    5.449  5.34002  
121   0.920000    5.449  5.01308  
122   0.860000    5.449  4.68614  
123   0.790000    5.449  4.30471  
124  33.110001   -0.004 -0.13244  
125  33.110001    0.005  0.16555  

样本数据:IV_start_index[:5]

[0, 124, 251, 381, 512]

基于@jezrael的回答,我已经成功创建了三个独立的系列。如何将它们附加到主数据框。下面的情节解释了我的要求。

【问题讨论】:

  • list(map(lambda x: x, ...))) 的意义何在?
  • a.tolist() 会返回一个类似[[....]] 的列表列表,也许你想做的是a.tolist()[0]
  • 以上代码存在于for 循环中。在这里,每次迭代的限制都在不断变化。因此,我为每次迭代创建了一组新的系列。在上面的代码中,我给出了特定迭代的值 (2,2,new_samples)(10,0,new_samples)
  • 您的系列ab 仅包含一个条目,并且该条目是10 个元素的列表,这是您想要的吗?
  • 为什么要映射到 lambda?

标签: python python-3.x dataframe lambda


【解决方案1】:

问题是每个系列都是一个带有列表的元素,因此不可能使用矢量化操作。

a = pd.Series([list(map(lambda x:x,np.linspace(2,2,new_samples)))],index=['Current'])
b = pd.Series([list(map(lambda x:x,np.linspace(10,0,new_samples)))],index=['Voltage'])

print (a)
Current    [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, ...
dtype: object

print (b)
Voltage    [10.0, 8.88888888888889, 7.777777777777778, 6....
dtype: object

所以我认为需要删除[] 并在必要时添加参数name

a = pd.Series(list(map(lambda x:x,np.linspace(2,2,new_samples))), name='Current')
b = pd.Series(list(map(lambda x:x,np.linspace(10,0,new_samples))),name='Voltage')
print (a)
0    2.0
1    2.0
2    2.0
3    2.0
4    2.0
5    2.0
6    2.0
7    2.0
8    2.0
9    2.0
Name: Current, dtype: float64

print (b)
0    10.000000
1     8.888889
2     7.777778
3     6.666667
4     5.555556
5     4.444444
6     3.333333
7     2.222222
8     1.111111
9     0.000000
Name: Voltage, dtype: float64

c = a * b
print (c)
0    20.000000
1    17.777778
2    15.555556
3    13.333333
4    11.111111
5     8.888889
6     6.666667
7     4.444444
8     2.222222
9     0.000000
dtype: float64

编辑:

如果想要输出乘以 Series 需要最后 2 行:

missing_current = pd.Series(list(map(lambda x:x,np.linspace(isc_act,isc_act,new_samples))))
missing_voltage = pd.Series(list(map(lambda x:x,np.linspace(module_allData_df['Voltage'].iloc[isc_indx],0,new_samples))))
print(missing_current *missing_voltage)

【讨论】:

    【解决方案2】:

    使用numpy 更容易。

    import numpy as np
    new_samples = 10 # Number of samples in series
    a = np.array(np.linspace(2,2,new_samples))
    b = np.array(np.linspace(10,0,new_samples))
    c = a*b
    print(c)
    

    输出:

    array([20., 17.77777778, 15.55555556, 13.33333333, 11.11111111, 8.88888889, 6.66666667, 4.44444444, 2.22222222, 0.])

    当您使用 pandas 数据框做所有事情时,请使用以下代码。

    import pandas as pd
    new_samples = 10 # Number of samples in series
    df = pd.DataFrame({'Current':np.linspace(2,2,new_samples),'Voltage':np.linspace(10,0,new_samples)})
    df['Power'] = df['Current'] * df['Voltage']
    print(df.to_string(index=False))
    

    输出:

    Current    Voltage      Power
        2.0  10.000000  20.000000
        2.0   8.888889  17.777778
        2.0   7.777778  15.555556
        2.0   6.666667  13.333333
        2.0   5.555556  11.111111
        2.0   4.444444   8.888889
        2.0   3.333333   6.666667
        2.0   2.222222   4.444444
        2.0   1.111111   2.222222
        2.0   0.000000   0.000000
    

    【讨论】:

    • 看起来很简单。但是,如何为每个列表分配一个名称,然后将此列表附加到数据框中已经存在的列?因为我的数据框有三列,其中包含一些数据df.columns = ['Current','Voltage','Power']。如何将上述数组a ('Current'),b('Voltage),c('Power') 附加到这些列?
    • 我可以像上面的代码那样把它放在一个for循环中吗?
    • 不,你不能。你把事情复杂化了。在去这些东西之前最好先正确学习pandas和python
    【解决方案3】:

    因为它们是系列,所以你应该能够将它们相乘 c= a * b

    您可以将 a 和 b 添加到数据框中,c 成为第三列

    【讨论】:

      猜你喜欢
      • 2019-11-10
      • 1970-01-01
      • 2015-04-04
      • 2013-11-03
      • 1970-01-01
      • 2014-03-16
      • 2020-09-10
      • 1970-01-01
      • 2017-02-17
      相关资源
      最近更新 更多