1

Trying to create a dataframe from multiple pandas series.

Here is my code

series = [pd.Series([nine, ten, eleven, twelve,thirteen], index=('link','post','shared','status','timeline'))]
my_names = [2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
my_series = [pd.Series([nine, ten, eleven, twelve,thirteen], index=('link','post','shared','status','timeline'), name=n) for n in my_names]

Here is a sample of the data series I am interacting with:

#nine
title
link       6.060606
post       8.080808
status    85.858586
dtype: float64

#ten
title
link         1.666667
post        15.833333
shared       1.666667
status      71.666667
timeline     9.166667
dtype: float64

#eleven
title
link        23.885350
shared       0.318471
status      59.872611
timeline    15.923567
dtype: float64

#twelve
title
link        18.660287
shared       3.588517
status      65.550239
timeline    12.200957
dtype: float64

The desired outcome is having 2009-2018 as the column titles, link, shared, status, timeline post as the row indexes and populating each row with the values from each of the series (nine =2009, ten=2010).

This is a sample of my current outcome:

sample

1 Answer 1

1

I believe you need concat with parameter keys for new column names:

ser = [nine, ten, eleven, twelve,thirteen]
my_names = [2009,2010,2011,2012,2013]

df = pd.concat(ser, axis=1, keys=my_names)

If input are lists:

nine = [4,5,6]
ten = [9,0,3]
eleven = [7,3,0]

L = [nine, ten, eleven]
my_names = [2009,2010,2011]

df = pd.DataFrame({k:v for k, v in zip(my_names, L)})
print (df)
   2009  2010  2011
0     4     9     7
1     5     0     3
2     6     3     0

Your solution return list of Series, so only need concat:

df = pd.concat(my_series, axis=1)

Or:

df = pd.DataFrame(my_series).T
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.