Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将两个已创建的系列相乘以创建第三个系列_Python_Python 3.x_Dataframe_Lambda - Fatal编程技术网

Python 如何将两个已创建的系列相乘以创建第三个系列

Python 如何将两个已创建的系列相乘以创建第三个系列,python,python-3.x,dataframe,lambda,Python,Python 3.x,Dataframe,Lambda,我已经创建了两个系列,我想通过对前两个进行元素相乘来创建第三个系列。我的代码如下: 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=[

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

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'
为了保持清楚,我将实际的
粘贴到下面的
循环代码中。我的数据框已经有三列
电流
电压
电源
。根据我的要求,我必须将新的值列表添加到现有列
Voltage
Current
。但是,
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()

示例数据: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_开始_索引[:5]

[0, 124, 251, 381, 512]

基于
@jezrael
答案,我成功地创建了三个独立的系列。如何将它们附加到主数据帧。我的要求在下面的图中解释

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


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

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


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

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

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

编辑:

如果想要超过,则需要最后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)

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

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

编辑:

如果想要超过,则需要最后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)

使用
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)
输出:

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
数组([20,17.777778,15.555556,13.33333, 11.11111111, 8.8888889、6.66666667、4.4444、2.2222222、0.)

当您使用pandas dataframe执行所有操作时,请使用以下代码

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

使用
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)
输出:

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
数组([20,17.777778,15.555556,13.33333, 11.11111111, 8.8888889、6.66666667、4.4444、2.2222222、0.)

当您使用pandas dataframe执行所有操作时,请使用以下代码

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

列表(map(lambda x:x,…)
的意义是什么?并且
a.tolist()
返回一个类似[[..]]的列表列表,您可能想做的是
a.tolist()[0]
上面的代码存在于
for
循环中。在这里,每次迭代的限制都会不断变化。因此,我为每个迭代创建一组新的序列。在上面的代码中,我给出了特定迭代的
(2,2,new_samples)
(10,0,new_samples)
。序列
a
b
只包含一个条目,条目是一个包含10个元素的列表,这就是您想要的吗?为什么要映射到lambda?列表的要点是什么(map(lambda x:x,…))
?和
a.tolist()
返回一个类似[[..]]的列表,您可能想做的是
a.tolist()[0]
上述代码存在于
for
循环中。在这里,每次迭代的限制都会不断变化。因此,我为每个迭代创建一组新的序列。在上面的代码中,我给出了特定迭代的
(2,2,新样本)
(10,0,新样本)
。序列
a
b
只包含一个条目,条目是一个包含10个元素的列表,这就是您想要的吗?为什么要映射到lambda?看起来很简单。但是,如何为每个列表指定一个名称,然后将该列表附加到数据帧中已经存在的列中?因为我的数据框有三列,其中有一些数据
df.columns=['Current','Voltage','Power']
。如何将上面的数组
a('Current')
b('Voltage)
c('Power')
附加到这些列中?我可以像上面代码中所示将其放入for循环中吗?不,不能。你把事情搞复杂了。在开始这项工作之前,最好先正确地学习熊猫和python,因为它看起来很简单。但是,如何为每个列表指定一个名称,然后将该列表附加到数据帧中已经存在的列中?因为我的数据框有三列,其中有一些数据
df.columns=['Current','Voltage','Power']
。如何将上面的数组
a('Current')
b('Voltage)
c('Power')
附加到这些列中?我可以像上面代码中所示将其放入for循环中吗?不,不能。你把事情搞复杂了。在学习这些东西之前,最好先正确地学习熊猫和蟒蛇