Posts

Showing posts with the label aggregate

Create new columns from aggregated categories

Image
Clash Royale CLAN TAG #URR8PPP Create new columns from aggregated categories I have a dataframe looks like: SK_ID_CURR CREDIT_ACTIVE 0 215354 Closed 1 215354 Active 2 215354 Active 3 215354 Active 4 215354 Active 5 215354 Active 6 215354 Active 7 162297 Closed 8 162297 Closed 9 162297 Active I would like to aggregate the number of active and closed credits for each id, and then make a new column for Active_credits , Closed_credits with the number of corresponding active and closed credits for each id. Active_credits Closed_credits What is your expected output? – Akshay Nevrekar 10 mins ago 2 Answers 2 You can use pandas.crosstab , which avoids your suggested intermediary step: pandas.crosstab res = pd.crosstab(df['...

Using the “aggregate” function for drawing line plots

Image
Clash Royale CLAN TAG #URR8PPP Using the “aggregate” function for drawing line plots I am trying to draw a line plot with error bars for two groups of data (Treatment vs. Control). There are totally 20 periods, 10 Trial Periods (TP) and 10 formal Periods (P) and I want to show how the group means change over time. For simplicity, the following dataframe includes 3 Trial Periods (TP1, TP5, TP10) and 3 formal Periods (P1, P5, P10). Below is my code. My problem is that the “aggregate” function changes the order of the periods by resorting them as strings, which messes up the time trend—I want them to be ordered as TP1->TP5->TP10->P1->P5->P10 I suppose this is not too tricky, but I’m just stuck. I’d appreciate it if someone could tell me how to solve this problem. Also: as there are 20 periods in total, it might look better to draw error bands (or CI bands) instead of numerous error bars. Is there a way to do this? df <- data.frame(Condition=c(rep("Treatment...