反转一系列列表

时间:2017-05-01 22:17:59

标签: python pandas series

我有以下Pandas系列名为Fruits:

Out[33]: 
Apples
0    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
1    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
2    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
3                            [0.0, 1.0, 2.0, 3.0, 4.0]
4    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
5    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
6    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
7                            [0.0, 1.0, 2.0, 3.0, 4.0]
8    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
9             [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
dtype: object

我想反转每一行(水平)。我使用代码Fruits [:: - 1],但输出与索引Apples(列)相反。一些想法可以扭转一系列的系列?

3 个答案:

答案 0 :(得分:0)

喜欢这个吗?

0   [1] [2] [3] [4] [5] 
1   [1] [2] [3] [4] [5] 
2   [1] [2] [3] [4] [5] 
3   [1] [2] [3] [4] [5] 
4   [1] [2] [3] [4] [5] 
=>
4   [5] [4] [3] [2] [1] 
3   [5] [4] [3] [2] [1] 
2   [5] [4] [3] [2] [1] 
1   [5] [4] [3] [2] [1] 
0   [5] [4] [3] [2] [1] 

你会得到:

select emplid, empl_rcd, company, plan_type, accrual_proc_dt, hrs_taken_unproc,  
       'N' as y_no_hrs_taken   --  is this really needed?
from   ps_leave_accrual
where  accrual_proc_dt > :1
  and  plan_type in ('50', '51')
  and  hrs_taken_unproc = 0
  and  (emplid, plan_type, accrual_proc_dt) 
         not in ( 
                  select emplid, plan_type, accrual_proc_dt
                  from   ps_y_sic_vac_hrs
                  where  accrual_proc_dt > :1
                    and  plan_type in ('50', '51')
                    and  hrs_taken_unproc = 0
                    and  y_no_hrs_taken   = 'Y'

                )
;

答案 1 :(得分:0)

s = pd.Series({0: [0, 1, 2, 3, 4],
 1: [0, 1, 2, 3, 4],
 2: [0, 1, 2, 3, 4],
 3: [0, 1, 2, 3, 4],
 4: [0, 1, 2, 3, 4]})

s.index.name='Apples'

print(s)
Apples
0    [0, 1, 2, 3, 4]
1    [0, 1, 2, 3, 4]
2    [0, 1, 2, 3, 4]
3    [0, 1, 2, 3, 4]
4    [0, 1, 2, 3, 4]
dtype: object

# use apply function to reverse the values row by row.
s.apply(lambda x: x[::-1])
Out[850]: 
Apples
0    [4, 3, 2, 1, 0]
1    [4, 3, 2, 1, 0]
2    [4, 3, 2, 1, 0]
3    [4, 3, 2, 1, 0]
4    [4, 3, 2, 1, 0]
dtype: object

答案 2 :(得分:0)

您似乎需要str[::-1]

Fruits = pd.Series(
[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2],
[0, 1, 2, 3, 4],
[0, 1]]).rename_axis('Apples')
print (Fruits)
Apples
0    [0, 1, 2, 3, 4]
1    [0, 1, 2, 3, 4]
2          [0, 1, 2]
3    [0, 1, 2, 3, 4]
4             [0, 1]
dtype: object

print(Fruits.str[::-1])
Apples
0    [4, 3, 2, 1, 0]
1    [4, 3, 2, 1, 0]
2          [2, 1, 0]
3    [4, 3, 2, 1, 0]
4             [1, 0]
dtype: object