Ernest Soo Ernest Soo - 1 year ago 148
Python Question

python pandas slice column

I have a pandas data structure with a shape of (500, 19).

dataset1 = pandas.read_csv(url, sep = ";", header = 0)


How can I slice the data vertically?

Example:

1,2,3
4,5,6
7,8,9


dataset1['slicing function to get second column']
yields 2,5,8

Answer Source

Use iloc function but python counts from 0, so for second column need 1:

print (df.iloc[:, 1])
0    2
1    5
2    8
Name: 1, dtype: int64

For range:

print (df.iloc[:, 0:3])
   0  1  2
0  1  2  3
1  4  5  6
2  7  8  9
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download