Reputation: 1281
Suppose I have a pd.DataFrame
object. Suppose further that I've used the iloc
method to extract a single row from it as a pd.Series
object. How can I transform that pd.Series
object into a single-row pd.DataFrame
object?
Here's a small reproducible example of what I'm talking about:
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,)
How can I reshape the row_as_series
object above into a single-row DataFrame? Here is what I'd like the output to look like
id colA colB
0 1 a 11
iloc[[0]]
I know that I can use iloc
and double square brackets to generate a DataFrame-like output instead of a Series-like output, as seen below:
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)
However, suppose I can't change the part of the code with the iloc
command. Suppose that I'm only allowed to edit the part of the code that is "downstream" from the iloc[]
method. I'm still curious about how to go from a Series object to a single-row DataFrame.
Upvotes: 0
Views: 450
Reputation: 323326
Alternative you can do groupby
with get_group
df.groupby(level=0).get_group(0)
id colA colB
0 1 a 11
If need covert
df.iloc[0].to_frame().T
Upvotes: 1
Reputation: 1281
You can just put the Series object inside a list and feed it into pd.DataFrame()
, as seen below:
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
with multiple rowsThis wasn't part of the original question, but it's worth mentioning: If you add multiple pd.Series
objects into the list, they will be read as multiple rows. Here's a quick example:
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)
Upvotes: 1