Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create DataFrame from multiple Series

I have 2 Series, given by:

import pandas as pd  r = pd.Series() for i in range(0, 10):     r = r.set_value(i,i*3) r.name = 'rrr'  s = pd.Series() for i in range(0, 10):     s = s.set_value(i,i*5) s.name = 'sss' 

How to I create a DataFrame from them?

like image 824
KcFnMi Avatar asked Oct 09 '16 07:10

KcFnMi


People also ask

Can a DataFrame contain multiple series?

Series can only contain single list with index, whereas dataframe can be made of more than one series or we can say that a dataframe is a collection of series that can be used to analyse the data.

How do you combine two series?

Combine Two Series Using pandas. merge() can be used for all database join operations between DataFrame or named series objects. You have to pass an extra parameter “name” to the series in this case. For instance, pd. merge(S1, S2, right_index=True, left_index=True) .

How do you create a DataFrame with rows as strides from a given series?

Here is how to create a DataFrame where each series is a row. To create a DataFrame where each series is a column, see the answers by others. Alternatively, one can create a DataFrame where each series is a row, as above, and then use df. transpose() .


1 Answers

You can use pd.concat:

pd.concat([r, s], axis=1) Out:     rrr  sss 0    0    0 1    3    5 2    6   10 3    9   15 4   12   20 5   15   25 6   18   30 7   21   35 8   24   40 9   27   45 

Or the DataFrame constructor:

pd.DataFrame({'r': r, 's': s})  Out:      r   s 0   0   0 1   3   5 2   6  10 3   9  15 4  12  20 5  15  25 6  18  30 7  21  35 8  24  40 9  27  45 
like image 152
ayhan Avatar answered Oct 05 '22 12:10

ayhan