【发布时间】: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。根据我的要求,我必须将新的值列表添加到现有列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()
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]
【问题讨论】:
-
list(map(lambda x: x, ...)))的意义何在? -
和
a.tolist()会返回一个类似[[....]] 的列表列表,也许你想做的是a.tolist()[0] -
以上代码存在于
for循环中。在这里,每次迭代的限制都在不断变化。因此,我为每次迭代创建了一组新的系列。在上面的代码中,我给出了特定迭代的值(2,2,new_samples)和(10,0,new_samples)。 -
您的系列
a和b仅包含一个条目,并且该条目是10 个元素的列表,这是您想要的吗? -
为什么要映射到 lambda?
标签: python python-3.x dataframe lambda