Python Forum
Why is first argument sometimes rows and sometimes columns?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is first argument sometimes rows and sometimes columns?
#1
I can do this:

# creating a value with all null values in new data frame 
new["Null Column"]= None
In this example, the first argument in brackets calls for a column.

I can also do this:
>>>df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
...  index=['cobra', 'viper', 'sidewinder'],
...  columns=['max_speed', 'shield'])

>>>df.loc['viper']
max_speed    4
shield       5
Name: viper, dtype: int64
In this example, the first argument in brackets references a row.

Is it accurate to say in more general terms that .loc and .iloc methods (and maybe others?) call for (row, column) whereas subsetting/slicing methods/syntax call for (column, row)?

If so, then is there a reason why it's not more consistent?
Reply
#2
You probably read the documentation (as this example is from there) but somehow missed concept of 'label' i.e. row and column names (two first rows in documentation of pd.DataFrame.loc):.

Quote:Access a group of rows and columns by label(s) or a boolean array.

.loc[] is primarily label based, but may also be used with a boolean array.

These labels are positional, which means that you can skip columns:

In [18]: df.loc['viper']       # no column labels specified, all are selected implicitly
Out[18]:
max_speed    3
shield       4
Name: viper, dtype: int64

In [19]: df.loc['viper', :]    # all column labels selected explicitly with :
Out[19]:
max_speed    3
shield       4
Name: viper, dtype: int64
But not rows:

In [20]: df.loc[:, 'shield']   # all rows are explicitly selected
Out[20]:
cobra         2
viper         4
sidewinder    6
Name: shield, dtype: int64

In [21]: df.loc['shield']      # no row label 'shield' (first positional argument)
/...../
KeyError: 'shield'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,162 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,798 Dec-12-2022, 08:22 PM
Last Post: jh67
  Check DataFrames with different sorting in columns and rows Foxyskippy 0 752 Nov-19-2022, 07:49 AM
Last Post: Foxyskippy
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,599 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  making variables in my columns and rows in python kronhamilton 2 1,576 Oct-31-2021, 10:38 AM
Last Post: snippsat
  rows from sql query need to write to a file as columns sjcsvatt 6 2,331 Oct-09-2021, 12:45 AM
Last Post: snippsat
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 8,355 Aug-19-2021, 03:08 PM
Last Post: johnbernard
  Summing up rows and columns plumberpy 3 2,219 Aug-18-2021, 05:46 AM
Last Post: naughtyCat
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,080 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,957 Mar-02-2021, 01:54 AM
Last Post: Jeremy7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020