Home > Blockchain >  Pandas Dataframe
Pandas Dataframe

Time:11-21

I am having problem in understanding this snippet of code.

import pandas as pd 


S1 = pd.Series([1, 2, 3, 4], index = ['a', 'b','c','d']) 


S2 = pd.Series([11, 22, 33, 44], index = ['a', 'bb','c','dd']) 


D1 = pd.DataFrame([S1,S2])

What I understand is there are two series and we are concatenating them to make a dataframe D. The result should be two columns and 6 rows.

With index of rows being : a b c d bb dd

The problem is that the answer in my book is given as 2 rows and 6 columns. How is it possible?

CodePudding user response:

When you build a dataFrame with a list of Pd.series objects like data, each element of the list is used to build a row of the resulting dataFrame. So since you are providing [S1, S2] as the data, your dataFrame will have S1 as first row, and S2 as second row. As far as columns are concerned, the columns of the dataFrame is the Union of the columns of S1 and those of S2, it is why you have 6 columns in the resulting dataFrame.

I think this link will better help you to understand.

pandas.Dataframe

  • Related