Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output a single row in pandas to an array

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?

like image 782
thejoker34 Avatar asked Feb 21 '18 01:02

thejoker34


People also ask

How do I fetch a row in Pandas?

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.

How do I slice a row into a DataFrame?

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.


1 Answers

Since Pandas data is stored internally as Numpy arrays, you can extract the Numpy representation directly.

v0.24+

Use pd.Series.to_numpy method:

df.iloc[3].to_numpy()  # output 4th row as Numpy array

Before v0.24

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.

like image 184
jpp Avatar answered Oct 06 '22 10:10

jpp