3

I am new to pandas,

I have the following dataframe:

df = pd.DataFrame([[1, 'name', 'peter'], [1, 'age', 23], [1, 'height', '185cm']], columns=['id', 'column','value'])

    id  column  value
0   1   name    peter
1   1   age     23
2   1   height  185cm

I need to create a single row for each ID. Like so:

   id  name   age  height
0  1   peter  23   185cm

Any help is greatly appreciated, thank you.

pandalover
  • 33
  • 4
  • 2
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jun 22 '17 at 10:22
  • 1
    Also [don't post images of code (or links to them)](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question) – jezrael Jun 22 '17 at 10:22
  • Yeh sorry , I didn't have enough reputation to embed an image – pandalover Jun 22 '17 at 10:27
  • Duplicate does not work. pivot_tables do not work with duplicate index and unstack/groupby is not working because you must use an agg_func and my values can be strings, thus you can't use "mean, sum" etc. – pandalover Jun 22 '17 at 11:04

2 Answers2

3

You can use pivot_table with aggregate join:

df = pd.DataFrame([[1, 'name', 'peter'], 
                   [1, 'age', 23], 
                   [1, 'height', '185cm'],
                   [1, 'age', 25]], columns=['id', 'column','value'])
print (df)
   id  column  value
0   1    name  peter
1   1     age     23
2   1  height  185cm
3   1     age     25

df1 = df.astype(str).pivot_table(index="id",columns="column",values="value",aggfunc=','.join)
print (df1)
    column    age height   name
id                         
1       23,25  185cm  peter

Another solution with groupby + apply join and unstack:

df1 = df.astype(str).groupby(["id","column"])["value"].apply(','.join).unstack(fill_value=0)
print (df1)
column    age height   name
id                         
1       23,25  185cm  peter
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Assuming your dataframe as "df", below line would help:

df.pivot(index="subject",columns="predicate",values="object")

MANI MARAN
  • 111
  • 1
  • 1
  • 6