Using two Pandas series: series1 , and series2 , I am willing to make series3 . Each value of series1 is a list, and each value of series2 is a corresponding index of 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
What I expect:
>>> 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...
My solution 1 : From the fact that the length of series1 and series2 are equal, I could make a for loop to iterate series1 and calculate something like series1.ix[i][series2.ix[i]]
and make a new series( series3 ) to save the result.
My solution 2 : Generate a dataFrame df using df = pd_concat([series1, series2])
, and make a new column(row-wise operation using apply function - eg, df['series3'] = df.apply(lambda x: subList(x), axis=1).
However, I thought above two solutions are not sharp ways to achieve what I want. I would appreciate if you suggest neater solutions!
If you are hoping to avoid creating an intermediate pd.DataFrame
, and simply want a new pd.Series
, you can use the pd.Series
constructor on a map
object. So given:
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
You can do:
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
If you want to modify S1
without creating an intermediate container, you can use a for-loop:
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
你基本上可以连接指定wich轴的系列(0 =行,1列),更好的是相同的长度
series3=pd.concat([series2, series1], axis=1).reset_index()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.