1

I have series which looks like this:

d1 = {'Class': 'A', 'age':35, 'Name': 'Manoj'}
d2 = {'Class': 'B', 'age':15, 'Name': 'Mot'}
d3 = {'Class': 'B', 'age':25, 'Name': 'Vittoo'}

ser = [d1, d2, d3]

dummy = pd.Series(ser)
dummy
0     {'Class': 'A', 'age': 35, 'Name': 'Manoj'}
1     {'Class': 'B', 'age': 15, 'Name': 'Mot'}
2    {'Class': 'B', 'age': 25, 'Name': 'Vittoo'}

When I use the to_frame function, it does this:

dummy.to_frame()

                      0
0   {'Class': 'A', 'age': 35, 'Name': 'Manoj'}
1   {'Class': 'B', 'age': 15, 'Name': 'Mot'}
2   {'Class': 'B', 'age': 25, 'Name': 'Vittoo'}

But what I intent to get is this:

Class   Name    age
0   A   Manoj   35
1   B   Mot     15
2   B   Vittoo  25

I have tried this which works fine:

df = pd.DataFrame(dummy)
df = df[0].apply(pd.Series)
df

But it feels very inefficient because I need to convert the Series to a dataframe and again apply the Series function to the complete dataframe. As I'm working with millions of rows, I'd like to know if there is a more efficient solution.

1 Answer 1

2

Use DataFrame constructor instead Series constructor:

d1 = {'Class': 'A', 'age':35, 'Name': 'Manoj'}
d2 = {'Class': 'B', 'age':15, 'Name': 'Mot'}
d3 = {'Class': 'B', 'age':25, 'Name': 'Vittoo'}

ser = [d1, d2, d3]

df = pd.DataFrame(ser)
print (df)
  Class    Name  age
0     A   Manoj   35
1     B     Mot   15
2     B  Vittoo   25

If input data is Series fiiled by dictionaries convert it to lists before DataFrame constructor, to_frame is not necessary:

dummy = pd.Series(ser)

df = pd.DataFrame(dummy.values.tolist())
print (df)
  Class    Name  age
0     A   Manoj   35
1     B     Mot   15
2     B  Vittoo   25
Sign up to request clarification or add additional context in comments.

2 Comments

I have directly added the Series dummy to the constructor previously, which gave the same result as to_frame. Should I convert the series values to a list and then use the DataFrame constructor?
@MohitMotwani - yes, exactly. df = pd.DataFrame(pd.Series(ser).values.tolist())

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.