Pandas file frame issue

I have this dictionary:

d = {1 : [ '2' , 'Pepsi' ], 3 : [ '1' , 'Pepper' ]}

df = pd.DataFrame([[slide, i[0], i[1]] for slide, item in d.items() for i in
item], columns=['Slide', 'Level', 'Level Title'])

      

I want to get the following output:

slide   level    'level1 title'   'level2 title'
1       2                          Pepsi
3       1         Pepper

      

While my code is outputting this:

slide   level    'level title'
1       2         Pepsi
3       1         Pepper

      

Basically, when the first item in the list is 1, the second item in the list should go to "level1 title", and when the item is 2, the second item should go to "level2 title"

+3


source to share


2 answers


If there are only two levels, you can use a list comprehension, for example:



In [12]: df = pd.DataFrame([[slide, item[0], item[1], ''] if item[0] == '1' else [slide, item[0], '', item[1]] 
for slide, item in d.items()], columns=['Slide', 'Level', 'Level1 Title', 'Level2 Title'])

In [13]: df
Out[13]:
   Slide Level Level1 Title Level2 Title
0      1     2                     Pepsi
1      3     1       Pepper

      

+1


source


I suggest you start with your DataFrame as you already got:

slide   level    level_title
1       2         Pepsi
3       1         Pepper

      

Add a couple of columns that have nothing:

In [24]: df['level1'] = pd.np.nan

In [25]: df['level2'] = pd.np.nan

      



Then set the values ​​as needed with some conditional magic:

In [40]: df.loc[df['level'] == 2, 'level1'] = df.level_title

In [41]: df.loc[df['level'] == 1, 'level2'] = df.level_title

In [42]: df
Out[42]: 
   slide  level level_title level1  level2
0      1      2       Pepsi  Pepsi     NaN
1      3      1      Pepper    NaN  Pepper

      

(Spot a "deliberate" mistake, I got the levels and headings stumped, but you get the idea!)

+1


source







All Articles