0

I have the following dataframe:

DataFrame

I want the output to be:

df_output

I can do it looping over the original dataframe and adding data to a new dataframe but I have the impression there must be a simpler approach. I have tried pivot_table, grouping,query and dictionaries but with no results. Any ideas? Thanks in advance.

2
  • Welcome to StackOverflow! Unfortunately, this site has rules about how questions should be asked, and this one does not meet them. As a new user, you really should read How to Ask. Here, you have provided links to images. Both are frowned upon here, because links force readers to change page to understand the question and image prevent copy/paste. You should at least copy both dataframes as text (formatted as code) in the question itself. Commented Jun 26, 2020 at 21:06
  • Hi @KarlManz, welcome to SO. in general avoid pasting images because they cannot be use to reproduce your data. Your previous version was actually ok, where I formatted your table. Other users can take that and try. Any reason for switching to the images? Commented Jun 26, 2020 at 21:07

1 Answer 1

1

You can set the 'Owner' to a category so that it will appear in the order after you pivot:

df = pd.DataFrame({'Owner':['John Doe','Woody Woodpecker','Bart Simpson','Ringo Star','Woody Woodpecker',
                           'John Doe','Donald Duck','Woody Woodpecker','John Doe','Bart Simpson'],
                   'Lot':['A%02d' % n for n in np.arange(1,11)]})
df['Owner'] = pd.Categorical(df['Owner'],categories=pd.unique(df['Owner']))

What you are missing is a column to enumerate the unique rows per group:

df['N'] = df.groupby('Owner').cumcount()+1

    Owner              Lot  N
0   John Doe           A01  1
1   Woody Woodpecker   A02  1
2   Bart Simpson       A03  1
3   Ringo Star         A04  1
4   Woody Woodpecker   A05  2
5   John Doe           A06  2
6   Donald Duck        A07  1
7   Woody Woodpecker   A08  3
8   John Doe           A09  3
9   Bart Simpson       A10  2

And changing this to the names you want, and you can pivot easily:

df['N'] = ['Lot'+str(i) for i in df['N']]
df.pivot(index='Owner',columns='N',values='Lot').reset_index()


N   Owner               Lot1 Lot2   Lot3
0   John Doe            A01  A06    A09
1   Woody Woodpecker    A02  A05    A08
2   Bart Simpson        A03  A10    NaN
3   Ringo Star          A04  NaN    NaN
4   Donald Duck         A07  NaN    NaN
Sign up to request clarification or add additional context in comments.

2 Comments

I apologize for the way I asked the question and its formatting and I thank the replies which I am going to test right now. Best regards.
Worked like a charm! Thanks a lot!

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.