Python Forum
Converting column of values into muliple columns of counts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting column of values into muliple columns of counts
#1
Hi everyone.

I have a dataframe in Python that has the following format:

Cyc MA
S1 1
S1 5
S2 7
S3 2
S3 6
S3 4
S4 13
S5 1
S5 1

I'm trying to convert it into the following format, where each cell represents the count of the number of times each Cyc value was each MA value. This would require making many new columns (basically between 1 and 13, the lowest and highest values of MA from the original table).

Cyc MA1 MA2 MA3 MA4 MA5 MA6 MA7 MA8 MA9 M10 M11 M12 M13
S1 1 0 0 0 1 0 0 0 0 0 0 0 0
S2 0 0 0 0 0 0 1 0 0 0 0 0 0
S3 0 1 0 1 0 1 0 0 0 0 0 0 0
S4 0 0 0 0 0 0 0 0 0 0 0 0 1
S5 2 0 0 0 0 0 0 0 0 0 0 0 0

I've tried a few things with "melting" the data frame and doing "group.by sums" -- but I was unable to entirely replicate this slightly more complicated scenario (i.e. would only solve a part of the problem).

Any suggestions or pointers would be so helpful. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 289 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,427 Oct-06-2023, 04:32 PM
Last Post: cubangt
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,522 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  PowerBI: Using Python Regex to look for values in column MarcusR44 1 982 Oct-14-2022, 01:03 PM
Last Post: ibreeden
  Read All Emails from Outlook and add the word counts to a DataFrame sanaman_2000 0 1,871 Sep-15-2022, 07:32 AM
Last Post: sanaman_2000
  How to combine multiple column values into 1? cubangt 15 2,872 Aug-11-2022, 08:25 PM
Last Post: cubangt
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,626 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  df column aggregate and group by multiple columns SriRajesh 0 1,052 May-06-2022, 02:26 PM
Last Post: SriRajesh
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,134 Apr-19-2022, 07:36 AM
Last Post: JulianZ
  Transform 3 Columns into Single Column DaveG 8 1,889 Apr-04-2022, 08:42 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020