使用两个Pandas系列: series1 和 series2 ,我愿意制作 series3 。 series1 的每个值都是一个列表, series2 的每个值都是series1的对应索引。
>>> print(series1)
0 [481, 12, 11, 220, 24, 24, 645, 153, 15, 13, 6...
1 [64, 80, 79, 147, 14, 20, 56, 288, 12, 208, 26...
4 [5, 6, 152, 31, 295, 127, 711, 5, 271, 291, 11...
5 [363, 121, 727, 249, 483, 122, 241, 494, 555]
7 [112, 20, 41, 9, 104, 131, 26, 298, 65, 214, 1...
9 [129, 797, 19, 151, 448, 47, 19, 106, 299, 144...
11 [72, 35, 25, 200, 122, 5, 75, 30, 208, 24, 14,...
18 [137, 339, 71, 14, 19, 54, 61, 15, 73, 104, 43...
>>> print(series2)
0 0
1 3
4 1
5 6
7 4
9 5
11 7
18 2
我的期望:
>>> print(series3)
0 [481, 12, 11, 220, 24, 24, 645, 153, 15, 13, 6...
1 [147, 14, 20, 56, 288, 12, 208, 26...
4 [6, 152, 31, 295, 127, 711, 5, 271, 291, 11...
5 [241, 494, 555]
7 [104, 131, 26, 298, 65, 214, 1...
9 [47, 19, 106, 299, 144...
11 [30, 208, 24, 14,...
18 [71, 14, 19, 54, 61, 15, 73, 104, 43...
我的解决方案1 : 由于 series1 和 series2 的长度相等,我可以使for循环迭代 series1 并计算类似{{1并创建一个新系列( series3 )来保存结果。
我的解决方案2 :
使用series1.ix[i][series2.ix[i]]
生成dataFrame df,并创建一个新列(使用apply函数的行方式操作 - 例如,df [' series3'] = df.apply(lambda x:subList(x) ,axis = 1)。
然而,我认为上面两种解决方案并不是实现我想要的方式。如果你建议更整洁的解决方案,我将不胜感激!
答案 0 :(得分:3)
如果您希望避免创建中间pd.DataFrame
,只需要新的pd.Series
,则可以在pd.Series
对象上使用map
构造函数。所以给出:
In [6]: S1
Out[6]:
0 [481, 12, 11, 220, 24, 24, 645, 153, 15, 13, 6]
1 [64, 80, 79, 147, 14, 20, 56, 288, 12, 208, 26]
2 [5, 6, 152, 31, 295, 127, 711, 5, 271, 291, 11]
3 [363, 121, 727, 249, 483, 122, 241, 494, 555]
4 [112, 20, 41, 9, 104, 131, 26, 298, 65, 214, 1]
5 [129, 797, 19, 151, 448, 47, 19, 106, 299, 144]
6 [72, 35, 25, 200, 122, 5, 75, 30, 208, 24, 14]
7 [137, 339, 71, 14, 19, 54, 61, 15, 73, 104, 43]
dtype: object
In [7]: S2
Out[7]:
0 0
1 3
2 1
3 6
4 4
5 5
6 7
7 2
dtype: int64
你可以这样做:
In [8]: pd.Series(map(lambda x,y : x[y:], S1, S2), index=S1.index)
Out[8]:
0 [481, 12, 11, 220, 24, 24, 645, 153, 15, 13, 6]
1 [147, 14, 20, 56, 288, 12, 208, 26]
2 [6, 152, 31, 295, 127, 711, 5, 271, 291, 11]
3 [241, 494, 555]
4 [104, 131, 26, 298, 65, 214, 1]
5 [47, 19, 106, 299, 144]
6 [30, 208, 24, 14]
7 [71, 14, 19, 54, 61, 15, 73, 104, 43]
dtype: object
如果您想在不创建中间容器的情况下修改S1
,可以使用for循环:
In [10]: for i, x in enumerate(map(lambda x,y : x[y:], S1, S2)):
...: S1.iloc[i] = x
...:
In [11]: S1
Out[11]:
0 [481, 12, 11, 220, 24, 24, 645, 153, 15, 13, 6]
1 [147, 14, 20, 56, 288, 12, 208, 26]
2 [6, 152, 31, 295, 127, 711, 5, 271, 291, 11]
3 [241, 494, 555]
4 [104, 131, 26, 298, 65, 214, 1]
5 [47, 19, 106, 299, 144]
6 [30, 208, 24, 14]
7 [71, 14, 19, 54, 61, 15, 73, 104, 43]
dtype: object
答案 1 :(得分:0)
你基本上可以结合指定轴的系列(0 =行,1列),最好是相同的长度
series3=pd.concat([series2, series1], axis=1).reset_index()