Why is first argument sometimes rows and sometimes columns? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Why is first argument sometimes rows and sometimes columns? (/thread-29444.html) |
Why is first argument sometimes rows and sometimes columns? - Mark17 - Sep-02-2020 I can do this: # creating a value with all null values in new data frame new["Null Column"]= NoneIn 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: int64In 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? RE: Why is first argument sometimes rows and sometimes columns? - perfringo - Sep-03-2020 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. 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: int64But 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' |