简体   繁体   中英

Converting Values of series with dictionary values to DataFrame. Not the Series itself

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM