0

If i have a dataframe called df I can select rows 1 to 4, using this df[1:4]

But I can't do it like this (it is possible in R)

a=[1:4]

df[a]

Is this possible in Pandas?

fred.schwartz
  • 2,023
  • 4
  • 26
  • 53
  • 2
    `a=[1:4]` is not valid python syntax so I'm not sure how you have a practical problem that would boil down to wanting to do this. Though, it did make me sit back for a second and think that it's not an unreasonable feature :) – roganjosh Sep 29 '18 at 14:13
  • yeah but something like this basically I have several dataframes that all need to be updated throughout the code. But it would be great just to change the range once – fred.schwartz Sep 29 '18 at 14:15
  • 4
    `a = slice(1, 4)`. For more complicated slices you can use [pd.IndexSlice](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.IndexSlice.html). – ayhan Sep 29 '18 at 14:17
  • ok thanks. I've done it like this in R: a=c(1:100) df=df[a,] – fred.schwartz Sep 29 '18 at 14:18
  • 1
    Actually, the suggestion by @ayhan is _much_ better – roganjosh Sep 29 '18 at 14:18
  • thanks ayhan. I'll give that a go – fred.schwartz Sep 29 '18 at 14:18

0 Answers0