1

I have the following code to adress columns and rows in a dataframe in Python:

y_train = features.iloc[start:end] [[1]]
y_train_noDoppleBracket = features.iloc[start:end] [1]

y_train_noIloc = features [start:end] [[1]]
y_train_noIloc_noDoppleBracket = features [start:end] [1]

In the cases without doppleBrackets I get a series of size (300693,) and in the cases with dopplBrackets I get a dataframe of size (300693,1). This also holds for the iloc examples. However if I have a look at them in the Variable Explorer of Spyder, they look exactly the same. So is there a difference between them? And why do I get a dataframe when using dopple brackets while only getting a series when using single brackets?

I'd appreciate every comment.

Reminder: As I still do not understand whether there is a difference between them, I would like to remind you on my question (the comments to this question say yes, but in the variable explorer they look exactly the same). I'd be quite happy for your help.

PeterBe
  • 700
  • 1
  • 17
  • 37
  • You could look at `type(x)` for `x` with or without double brackets. – hilberts_drinking_problem Oct 23 '20 at 07:59
  • Thanks hilberts for your answer. What do you mena by that? – PeterBe Oct 23 '20 at 08:09
  • Yes there's a difference. Double brackets will always return a `DataFrame` object - you're essentially passing in a list of column names. eg `df[['col1', 'col2']]`. That list can of course only contain a single column, but it will still return a `DataFrame`. Single square brackets is the syntax for indexing a single column and will always return a `Series` object eg. `df['col1']`. – Chris Adams Oct 23 '20 at 08:20
  • I suppose that you will find `type(y_train)` to be of `DataFrame` type and `y_train_noDoppleBracket` to be of `Series` type. When using double brackets, you get a `DataFrame` with one column. These are definitely distinct Python objects. There are many reasons for allowing this behavior; here is [one](https://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r). – hilberts_drinking_problem Oct 23 '20 at 08:20
  • Thanks for your answers Chris and Hilberts. Is there a difference between a 1-dimensional dataframe and a series? As stated in the question, in the variable explorer they look exactly the same. They both have multiple rows (with an index) in one column – PeterBe Oct 23 '20 at 09:19
  • Any further comments? I still do not know whether there is a difference as in the variable explorer they look exactly the same – PeterBe Oct 26 '20 at 16:11
  • @PeterBe there *is no such thing as a one-dimensional dataframe*. `(x, 1)` has two dimensions. In any case, yes, they are of two different types, so they might be mostly the same for your purposes but there will be subtle differences. "it looks the same in the variable explorer" is practically irrelevant. – juanpa.arrivillaga Oct 29 '20 at 18:01

0 Answers0