Reputation: 113
Find it little strange that slicing with -ve number and a list made out of that slice aren't matching. Why is it so?
dfl = DataFrame(np.random.randn(5,2),columns=list('AB'))
>>> dfl
A B
0 -0.775699 -0.622863
1 2.418822 0.747961
2 1.000536 0.036125
3 0.394238 -0.215857
4 0.374322 0.795460
>>> dfl.iloc[:,-1:]
B
0 -0.622863
1 0.747961
2 0.036125
3 -0.215857
4 0.795460
>>> list(dfl.iloc[:,-1:])
['B']
>>> list(dfl.iloc[:,1])
[-0.622863, 0.747961, 0.036125, -0.215857, 0.795460]
Upvotes: 1
Views: 91
Reputation: 394031
The reason is because your first slicing returns a DataFrame whilst the latter returns a Series:
In [183]:
type(dfl.iloc[:,-1:])
Out[183]:
pandas.core.frame.DataFrame
In [184]:
type(dfl.iloc[:,1])
Out[184]:
pandas.core.series.Series
Calling list on a df returns the columns
In [189]:
list(dfl)
Out[189]:
['A', 'B']
Upvotes: 1