将 Pandas 中的一行输出到一个数组

the*_*r34 7 python arrays indexing numpy pandas

我希望能够拉出任意行,例如第 4 行,并获取一个数组,以便我可以通过另一个函数发送它。

什么是最简单的方法来做到这一点?

jpp*_*jpp 15

由于 Pandas 数据在内部存储为 Numpy 数组,因此您可以直接提取 Numpy 表示。

v0.24+

使用pd.Series.to_numpy方法:

df.iloc[3].to_numpy()  # output 4th row as Numpy array
Run Code Online (Sandbox Code Playgroud)

v0.24之前

使用pd.Series.values属性:

df.iloc[3].values  # output 4th row as Numpy array
Run Code Online (Sandbox Code Playgroud)

请记住,如果您的数据框包含多种类型,dtype则行式 Numpy 数组的 可能会变为object. 这将导致后续操作效率低下。