Create pandas DataFrame from Series in Python (Example Code)
This article illustrates how to transform a pandas Series to a DataFrame column in the Python programming language.
Example Data
import pandas as pd # Import pandas |
import pandas as pd # Import pandas
srs = pd.Series(['x', 'a', 'f', 'x', 'x', 'f']) # Construct pandas Series print(srs) # 0 x # 1 a # 2 f # 3 x # 4 x # 5 f # dtype: object |
srs = pd.Series(['x', 'a', 'f', 'x', 'x', 'f']) # Construct pandas Series print(srs) # 0 x # 1 a # 2 f # 3 x # 4 x # 5 f # dtype: object
Example: Convert pandas Series to DataFrame Column
df = srs.to_frame('my_col') # Apply to_frame() function print(df) # my_col # 0 x # 1 a # 2 f # 3 x # 4 x # 5 f |
df = srs.to_frame('my_col') # Apply to_frame() function print(df) # my_col # 0 x # 1 a # 2 f # 3 x # 4 x # 5 f
Further Resources
Below, you may find some further resources on topics such as lists and data conversion.