I would like to be able to pull out an arbitrary row, such as the 4th row, and get an array so that I can send it through another function.
What would be the easiest way to do this?
To get the nth row in a Pandas DataFrame, we can use the iloc() method. For example, df. iloc[4] will return the 5th row because row numbers start from 0.
Slicing Rows and Columns by Index PositionWhen slicing by index position in Pandas, the start index is included in the output, but the stop index is one step beyond the row you want to select. So the slice return row 0 and row 1, but does not return row 2. The second slice [:] indicates that all columns are required.
Since Pandas data is stored internally as Numpy arrays, you can extract the Numpy representation directly.
Use pd.Series.to_numpy
method:
df.iloc[3].to_numpy() # output 4th row as Numpy array
Use pd.Series.values
property:
df.iloc[3].values # output 4th row as Numpy array
Just remember that if your dataframe contains multiple types, the dtype
of the row-wise Numpy array may become object
. This will lead to inefficiencies in subsequent operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With