如何将系列对象(使用 iloc 生成)转换为单行数据框?
How can I transform a series object (generated with iloc) into a single-row dataframe?
假设我有一个 pd.DataFrame
对象。进一步假设我使用 iloc
方法从中提取单行作为 pd.Series
对象。如何将 pd.Series
对象转换为单行 pd.DataFrame
对象?
这是我正在谈论的一个可重现的小例子:
import pandas as pd
# Generating a DataFrame
df = pd.DataFrame({'id':[1,2,3,4,5],
'colA':['a','b','c','d','e'],
'colB':[11,22,33,44,55]})
# Extracting a single row from the DataFrame as
# a pd.Series object
row_as_series = df.iloc[0]
print(row_as_series)
# id 1
# colA a
# colB 11
# Name: 0, dtype: object
print(type(row_as_series))
# <class 'pandas.core.series.Series'>
print(row_as_series.shape)
# (3,)
如何将上面的 row_as_series
对象重塑为单行 DataFrame?这是我希望输出的样子
id colA colB
0 1 a 11
关于 iloc[[0]]
的注意事项
我知道我可以使用 iloc
和双方括号来生成类似 DataFrame 的输出而不是类似 Series 的输出,如下所示:
row_as_df = df.iloc[[0]]
print(row_as_df)
# id colA colB
# 0 1 a 11
print(type(row_as_series))
# <class 'pandas.core.frame.DataFrame'>
print(row_as_series.shape)
# (1, 3)
但是,假设我无法使用 iloc
命令更改部分代码。假设我只被允许编辑 iloc[]
方法“下游”的代码部分。我仍然很好奇如何从 Series 对象转到单行 DataFrame。
您可以将 Series 对象放入列表中并将其输入 pd.DataFrame()
,如下所示:
single_row_df = pd.DataFrame([row_as_series])
print(single_row_df)
# id colA colB
# 0 1 a 11
print(type(single_row_df))
# <class 'pandas.core.frame.DataFrame'>
print(single_row_df.shape)
# (1, 3)
额外信息:建筑物 pd.DataFrames
有多行
这不是原始问题的一部分,但值得一提:如果将多个 pd.Series
对象添加到列表中,它们将被读取为多行。这是一个简单的例子:
temp_df = pd.DataFrame([df.iloc[0],
df.iloc[2],
df.iloc[4]])
print(temp_df)
# id colA colB
# 0 1 a 11
# 2 3 c 33
# 4 5 e 55
print(type(temp_df))
# <class 'pandas.core.frame.DataFrame'>
print(temp_df.shape)
# (3, 3)
您可以选择 groupby
和 get_group
df.groupby(level=0).get_group(0)
id colA colB
0 1 a 11
如需隐蔽
df.iloc[0].to_frame().T
假设我有一个 pd.DataFrame
对象。进一步假设我使用 iloc
方法从中提取单行作为 pd.Series
对象。如何将 pd.Series
对象转换为单行 pd.DataFrame
对象?
这是我正在谈论的一个可重现的小例子:
import pandas as pd
# Generating a DataFrame
df = pd.DataFrame({'id':[1,2,3,4,5],
'colA':['a','b','c','d','e'],
'colB':[11,22,33,44,55]})
# Extracting a single row from the DataFrame as
# a pd.Series object
row_as_series = df.iloc[0]
print(row_as_series)
# id 1
# colA a
# colB 11
# Name: 0, dtype: object
print(type(row_as_series))
# <class 'pandas.core.series.Series'>
print(row_as_series.shape)
# (3,)
如何将上面的 row_as_series
对象重塑为单行 DataFrame?这是我希望输出的样子
id colA colB
0 1 a 11
关于 iloc[[0]]
的注意事项
我知道我可以使用 iloc
和双方括号来生成类似 DataFrame 的输出而不是类似 Series 的输出,如下所示:
row_as_df = df.iloc[[0]]
print(row_as_df)
# id colA colB
# 0 1 a 11
print(type(row_as_series))
# <class 'pandas.core.frame.DataFrame'>
print(row_as_series.shape)
# (1, 3)
但是,假设我无法使用 iloc
命令更改部分代码。假设我只被允许编辑 iloc[]
方法“下游”的代码部分。我仍然很好奇如何从 Series 对象转到单行 DataFrame。
您可以将 Series 对象放入列表中并将其输入 pd.DataFrame()
,如下所示:
single_row_df = pd.DataFrame([row_as_series])
print(single_row_df)
# id colA colB
# 0 1 a 11
print(type(single_row_df))
# <class 'pandas.core.frame.DataFrame'>
print(single_row_df.shape)
# (1, 3)
额外信息:建筑物 pd.DataFrames
有多行
这不是原始问题的一部分,但值得一提:如果将多个 pd.Series
对象添加到列表中,它们将被读取为多行。这是一个简单的例子:
temp_df = pd.DataFrame([df.iloc[0],
df.iloc[2],
df.iloc[4]])
print(temp_df)
# id colA colB
# 0 1 a 11
# 2 3 c 33
# 4 5 e 55
print(type(temp_df))
# <class 'pandas.core.frame.DataFrame'>
print(temp_df.shape)
# (3, 3)
您可以选择 groupby
和 get_group
df.groupby(level=0).get_group(0)
id colA colB
0 1 a 11
如需隐蔽
df.iloc[0].to_frame().T