如何从 DataFrame 中提取一行作为 Series,并将 DataFrame 中的列名作为 Series 中的行索引?

How can I extract a row from a DataFrame as a Series, with the column names in the DataFrame as the row indices in the Series?

假设我有以下数据框:

   x y z
a  0 1 2
b  3 4 5
c  6 7 8

我怎样才能将行 b 提取为 Series 这样我现在有:

x 3
y 4
z 5

loc 将 return 一个系列,当你给它一个标签时。

import pandas as pd

df = pd.DataFrame({'x': [0, 3, 6],
                   'y': [1, 4, 7],
                   'z': [2, 5, 8]},
                  index=['a', 'b', 'c'])

s = df.loc['b']
print(type(s))
print(s)

输出:

<class 'pandas.core.series.Series'>
x    3
y    4
z    5
Name: b, dtype: int64