How to Convert Series to Row of Dataframe in Python
To convert a series to a row of a dataframe in Python, you can use the DataFrame() and transpose() functions.
The following example shows how to convert a series to a row of a dataframe in Python using the DataFrame() and transpose() functions.
Using DataFrame() & transpose() Function
We can use the DataFrame() and transpose() functions to convert a series to a row of a dataframe.
Suppose we have the following series:
import pandas as pd
# Create series
series_ = pd.Series({"Date": '03-03-2023', "Product_code": 'B-105', "Product_name": 'Monitor', "Price": 3500, "Status": 1})
# Convert series to dataframe with single row
df = pd.DataFrame(series_).transpose()
# Show dataframe
print(df)
Output: 👇️
Date Product_code Product_name Price Status
0 03-03-2023 B-105 Monitor 3500 1
In this example, we use the DataFrame() function to convert the series series_ to a dataframe and then use the transpose() function to convert it to a single row. The output shows the dataframe created with a single row from the series.
Conclusion
We can use the DataFrame() and transpose() functions to convert a series to a row of a dataframe in Python. This method provides a convenient way to reshape series data into a dataframe row.