Pandas 数据框行提取正在改变维度

Pandas dataframe row extraction is changing dimensions

我有一个 [2 行 x 51 列] 数据帧读入一个名为“csv”的变量。 我想从中创建 2 个新的 csv,基于行。 第一个 csv 变为 csv_row1 将是 [1 行 x 51 列] 第二个 csv 变为 csv_row2,这将是另一个 [1 行 x 51 列]。

我只写了一行代码:

firstrow = csv.loc[0]
firstrowindex = len(firstrow.index)
print(firstrowindex)

#output 51 

因此,当我期待查看 [1 行 x 51 列] 数据框时,我却看到了 [51 行 x 2 列]

我该怎么做?如何提取整行数据并仅为该行创建一个单独的 DataFrame?对每一行都这样做吗?

要将行作为 DataFrame 获取,您需要使用:

csv_row1 = csv.loc[[0]]