1

I need some help manipulating the contents of a .csv file.

My file looks like this:

CSV

I need to group the contents by "Council District" and then by "TYPENAME" and display their totals. I then need to store the result in 3 different columns like so:

grouped

I tried accomplishing this with this code:

ds=pd.read_csv(r"myfile.csv")
ds.info()
ds.keys()

ds=pd.read_csv("myfile.csv")
grouped=(ds.groupby(['Council District','TYPENAME']).size().to_frame("Count"))
grouped.to_csv(r"request_by_districtTEST.csv")
print(grouped.head())
grouped.describe(include='all')
grouped.keys()

But instead of having 3 separate columns I only get one: enter image description here

Anyone knows how can I fix my code so it stores it into 3 separate columns?

Ghost
  • 249
  • 1
  • 14
JPnova87
  • 43
  • 5
  • [Stack Overflow Discourages Screenshots](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). It is likely the question will be downvoted. You are discouraging assistance because no one wants to retype your data or code, and screenshots are often illegible. – Trenton McKinney Nov 24 '19 at 04:18
  • Please [provide a reproducible copy of the DataFrame with `to_clipboard`](https://stackoverflow.com/questions/52413246/provide-a-reproducible-copy-of-the-dataframe-with-to-clipboard/52413247#52413247) – Trenton McKinney Nov 24 '19 at 04:19
  • 1
    `g = df.groupby(['Council District','TYPENAME']).size().to_frame('count').reset_index()` then `g.to_csv('my.csv', index=False)`. – Trenton McKinney Nov 24 '19 at 04:31
  • Possible duplicate of [pandas groupby without turning grouped by column into index](https://stackoverflow.com/questions/32059397/pandas-groupby-without-turning-grouped-by-column-into-index) – Trenton McKinney Nov 24 '19 at 04:32
  • @TrentonMcKinney Thank you very much! that solved my problem!!! Will mark as answer!!! – JPnova87 Nov 24 '19 at 05:33

1 Answers1

0

The Answer is from the comment by Trenton McKinney

g = df.groupby(['Council District','TYPENAME']).size().to_frame('count').reset_index()
g.to_csv('my.csv', index=False)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
JPnova87
  • 43
  • 5