3

I'm using Pandas (and programming in general) for the first time and I could really use some help. From the data, I managed to create a variable x of type pandas.core.series.Series using value_counts that looks like this:

week        
0     SanDOW    20
      SanPCF    18
      SanSOT    10
      SanMSS     7
      SanPEN     2
...
52    SanCOL    2
      SanOCN    2
      SanSOT    2
      SanKER    1
      SanLAJ    1
      SanRNC    1
      SanCAR    1
53    SanDOW    1
Length: 1954, dtype: int64

Can you please assist/orient/help me in creating a dataframe that has the week (index) as columns? That way I can have a table that looks like this

        0   1   2   3 ... 53

SanDOW  
SanPCF
SanXXX
.
.
.

And the appropriate values for each city by week?

Apologies in advance if I'm not following proper etiquette when asking this question.

3
  • I think you demonstrated SO "etiquette" perfectly. Commented Jul 14, 2014 at 23:08
  • The question is nicely written, but it might help to edit the question title to be more specific about your question, both to help get knowledgable people interested in answering it and to help people who are searching for this specific issue in the future. Commented Jul 14, 2014 at 23:31
  • Thanks for the feedback, @mwaskom. I tried to improve the title based on the answer. Commented Jul 15, 2014 at 2:35

1 Answer 1

1

Assuming that your two first columns form a MultiIndex, you can simply use unstack:

x.unstack(level=0)

to push the first index level to be a column.

Sign up to request clarification or add additional context in comments.

Comments

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.