Convert Pandas Series to Dataframe

Convert pandas Series to DataFrame

Rather than create 2 temporary dfs you can just pass these as params within a dict using the DataFrame constructor:

pd.DataFrame({'email':sf.index, 'list':sf.values})

There are lots of ways to construct a df, see the docs

Converting pandas.core.series.Series to dataframe with appropriate column values python

You was very close, first to_frame and then transpose by T:

s = pd.Series([1159730, 1], index=['product_id_y','count'], name=6159402)
print (s)
product_id_y 1159730
count 1
Name: 6159402, dtype: int64

df = s.to_frame().T
print (df)
product_id_y count
6159402 1159730 1

df = s.rename(None).to_frame().T
print (df)
product_id_y count
0 1159730 1

Another solution with DataFrame constructor:

df = pd.DataFrame([s])
print (df)
product_id_y count
6159402 1159730 1

df = pd.DataFrame([s.rename(None)])
print (df)
product_id_y count
0 1159730 1

How can I turn a pandas.core.series.Series into a Dataframe?

Let us do

out = pd.DataFrame(s.tolist(), index = s.index)

How to convert pandas Series to DataFrame

Use to_frame and transpose your new dataframe:

df = sr.to_frame(0).T
print(df)

# Output:
wind humidity weather temp conclusion
0 yes high sunny hot bad

Setup

data = {'wind': 'yes',
'humidity': 'high',
'weather': 'sunny',
'temp': 'hot',
'conclusion': 'bad'}
sr = pd.Series(data, name=1)
print(sr)

# Output
wind yes
humidity high
weather sunny
temp hot
conclusion bad
Name: 1, dtype: object

Pandas Series to Pandas Dataframe

Use Series.to_frame:

pd.DataFrame(cfd_appr).transpose().sum(axis=1).to_frame(1)

Convert pandas series into dataframe

Your original code doesn't work because the indexing is wrong. You could fix it by dropping the index and only using the values, like this:

dfNEW = pd.DataFrame(columns = ['appID', 'rel', 'au']) # creates empty dataframe
dfNEW['appID'] = dfTMP.iloc[0::3].values
# and so on

But a much more compact way that works in cases like your example is:

dfNEW = pd.DataFrame(dfTMP.values.reshape(-1,3), columns=['appID', 'rel', 'au'])

pandas Series to Dataframe using Series indexes as columns

You can also try this :

df = DataFrame(series).transpose()

Using the transpose() function you can interchange the indices and the columns.
The output looks like this :

    a   b   c
0 1 2 3


Related Topics



Leave a reply



Submit