Tricks of Slicing a Series into subsets in Pandas
Slicing a Series into subsets
Slicing is a powerful approach to retrieve subsets of data from a pandas object. A slice object is built using a syntax of start:end:step, the segments representing the first item, last item, and the increment between each item that you would like as the step.
import pandas as pd num = [000, 100, 200, 300, 400, 500, 600, 700, 800, 900] idx = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] series = pd.Series(num, index=idx) print("\n [2:2] \n") print(series[2:4]) print("\n [1:6:2] \n") print(series[1:6:2]) print("\n [:6] \n") print(series[:6]) print("\n [4:] \n") print(series[4:]) print("\n [:4:2] \n") print(series[:4:2]) print("\n [4::2] \n") print(series[4::2]) print("\n [::-1] \n") print(series[::-1])
C:\python\pandas examples>python example1f.py [2:2] C 200 D 300 dtype: int64 [1:6:2] B 100 D 300 F 500 dtype: int64 [:6] A 0 B 100 C 200 D 300 E 400 F 500 dtype: int64 [4:] E 400 F 500 G 600 H 700 I 800 J 900 dtype: int64 [:4:2] A 0 C 200 dtype: int64 [4::2] E 400 G 600 I 800 dtype: int64 [::-1] J 900 I 800 H 700 G 600 F 500 E 400 D 300 C 200 B 100 A 0 dtype: int64 C:\python\pandas examples>
2018-10-12T23:16:29+05:30
2018-10-12T23:16:29+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution