Position-based selection of a DataFrame
Much like with a pd.Series
, integers, lists of integers, and slice objects are all valid arguments to DataFrame.iloc
. However, with a pd.DataFrame
, two arguments are required. The first argument handles selecting from the rows, and the second is responsible for the columns.
In most use cases, users reach for position-based selection when retrieving rows and label-based selection when retrieving columns. We will cover the latter in the Label-based selection from a DataFrame section and will show you how to combine both in the Mixing position-based and label-based selection section. However, when your row index uses the default pd.RangeIndex
and the order of columns is significant, the techniques shown in this section will be of immense value.
How to do it
Let’s create a pd.DataFrame
with five rows and four columns:
df = pd.DataFrame(np.arange(20).reshape(5, -1), columns=list("abcd"))
df
a b c...