Answers for "how to convert a series to dataframe"

4

python pandas series to dataframe

>>> s = pd.Series(["a", "b", "c"],
...               name="vals")
>>> s.to_frame()
  vals
0    a
1    b
2    c
Posted by: Guest on June-08-2020
0

pd df to series

import pandas as pd

data = {'First_Name': ['Jeff','Tina','Ben','Maria','Rob']}
df = pd.DataFrame(data, columns = ['First_Name'])

print(df)
#	First_Name
#0       Jeff
#1       Tina
#2        Ben
#3      Maria
#4        Rob

print(type(df))
# <class 'pandas.core.frame.DataFrame'>

df = df.squeeze() # <- Converts to a pandas series
print(df)
#0     Jeff
#1     Tina
#2      Ben
#3    Maria
#4      Rob
#Name: First_Name, dtype: object

print(type(df))
# <class 'pandas.core.series.Series'>
Posted by: Guest on June-22-2021

Code answers related to "how to convert a series to dataframe"

Python Answers by Framework

Browse Popular Code Answers by Language