Skip to content

Instantly share code, notes, and snippets.

@RensDimmendaal
Last active September 14, 2021 14:04
Show Gist options
  • Save RensDimmendaal/60eb1baa9661a187515d407366e72abc to your computer and use it in GitHub Desktop.
Save RensDimmendaal/60eb1baa9661a187515d407366e72abc to your computer and use it in GitHub Desktop.
pandas
def explode_list_of_dicts(df,col):
exploded_rows = df.explode(col)
exploded_cols = exploded_rows[col].apply(pd.Series)
return pd.concat([
exploded_rows.drop(col,axis=1),
exploded_cols
],
axis=1
)
# a nice dummy dataframe for debugging
import pandas as pd
heroes = pd.read_json('{"name":{"0":"Abathur","1":"Anub\'arak","2":"Artanis","3":"Arthas","4":"Azmodan","5":"Brightwing","6":"Chen","7":"Cho","8":"Dehaka","9":"Diablo","10":"E.T.C.","11":"Falstad","12":"Gall","13":"Gazlowe","14":"Greymane","15":"Illidan","16":"Jaina","17":"Johanna","18":"Kael\'thas","19":"Kerrigan","20":"Kharazim","21":"Leoric","22":"Li Li","23":"Li-Ming","24" :"Lt. Morales","25":"Lunara","26":"Malfurion","27":"Muradin","28":"Murky","29":"Nazeebo","30":"Nova","31":"Raynor","32":"Rehgar","33":"Rexxar","34":"Sgt. Hammer","35":"Sonya","36":"Stitches","37":"Sylvanas","38":"Tassadar","39":"The Butcher","40":"The Lost Vikings","41":"Thrall","42":"Tychus","43":"Tyrael","44":"Tyrande","45":"Uther","46":"Valla","47":"Xul","48":"Zagara","49":"Zeratul"},"hp":{"0":685,"1":1926,"2":2335,"3":2382,"4":2738,"5":1470,"6":2556,"7":3528,"8":2434,"9":2647,"10":2556,"11":1365,"12":0,"13":1826,"14":1876,"15":1426,"16":1365,"17":2451,"18":1465,"19":1680,"20":1949,"21":2373,"22":1552,"23":1232,"24":1528,"25":1430,"26":1682,"27":2556,"28":634,"29":1638,"30":1350,"31":1302,"32":2049,"33":1643,"34":1638,"35":2341,"36":2738,"37":1456,"38":1426,"39":2154,"40":1424,"41":1787,"42":1545,"43":2373,"44":1591,"45":2054,"46":1273,"47":1900,"48":1643,"49":1545},"attack":{"0":26.0,"1":99.0,"2":111.0,"3":99.0,"4":85.0,"5":85.0,"6":84.0,"7":120.0,"8":110.0,"9":118.0,"10":99.0,"11":104.0,"12":null,"13":95.0,"14":145.0,"15":78.0,"16":60.0,"17":99.0,"18":65.0,"19":109.0,"20":63.0,"21":138.0,"22":63.0,"23":63.0,"24":88.0,"25":90.0,"26":67.0,"27":97.0,"28":60.0,"29":88.0,"30":105.0,"31":122.0,"32":95.0,"33":99.0,"34":133.0,"35":88.0,"36":85.0,"37":74.0,"38":69.0,"39":169.0,"40":52.0,"41":165.0,"42":44.0,"43":78.0,"44":87.0,"45":78.0,"46":94.0,"47":104.0,"48":95.0,"49":126.0},"attack_spd":{"0":1.43,"1":1.0,"2":1.0,"3":1.0,"4":1.0,"5":1.11,"6":1.11,"7":0.91,"8":1.11,"9":0.91,"10":1.0,"11":1.43,"12":0.0,"13":1.25,"14":1.0,"15":1.67,"16":1.0,"17":0.91,"18":1.11,"19":1.25,"20":2.0,"21":0.77,"22":1.25,"23":1.0,"24":1.0,"25":1.11,"26":1.11,"27":1.11,"28":1.25,"29":1.11,"30":1.0,"31":1.25,"32":1.25,"33":0.87,"34":1.0,"35":1.25,"36":0.91,"37":1.67,"38":1.0,"39":1.11,"40":1.0,"41":0.91,"42":0.67,"43":1.25,"44":1.25,"45":1.0,"46":1.67,"47":1.0,"48":1.25,"49":1.11},"attack_type":{"0":"Melee","1":"Melee","2":"Melee","3":"Melee","4":"Ranged","5":"Ranged","6":"Melee","7":"Melee","8":"Melee","9":"Melee","10":"Melee","11":"Ranged","12":"Ranged","13":"Melee","14":"Ranged","15":"Melee","16":"Ranged","17":"Melee","18":"Ranged","19":"Melee","20":"Melee","21":"Melee","22":"Ranged","23":"Ranged","24":"Ranged","25":"Ranged","26":"Ranged","27":"Melee","28":"Melee","29":"Ranged","30":"Ranged","31":"Ranged","32":"Melee","33":"Ranged","34":"Ranged","35":"Melee","36":"Melee","37":"Ranged","38":"Ranged","39":"Melee","40":"Melee","41":"Melee","42":"Ranged","43":"Melee","44":"Ranged","45":"Melee","46":"Ranged","47":"Melee","48":"Ranged","49":"Melee"},"role":{"0":"Specialist","1":"Warrior","2":"Warrior","3":"Warrior","4":"Specialist","5":"Support","6":"Warrior","7":"Warrior","8":"Warrior","9":"Warrior","10":"Warrior","11":"Assassin","12":"Assassin","13":"Specialist","14":"Assassin","15":"Assassin","16":"Assassin","17":"Warrior","18":"Assassin","19":"Assassin","20":"Support","21":"Warrior","22":"Support","23":"Assassin","24":"Support","25":"Assassin","26":"Support","27":"Warrior","28":"Specialist","29":"Specialist","30":"Assassin","31":"Assassin","32":"Support","33":"Warrior","34":"Specialist","35":"Warrior","36":"Warrior","37":"Specialist","38":"Support","39":"Assassin","40":"Specialist","41":"Assassin","42":"Assassin","43":"Warrior","44":"Support","45":"Support","46":"Assassin","47":"Specialist","48":"Specialist","49":"Assassin"}}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment