pivot

Need to pivot pandas dataframe

Need to pivot pandas dataframe Question: I downloaded a csv from the world bank website that has stats for Australia going back to 1960(https://data.worldbank.org/country/australia) . I have read the csv in to a pandas dataframe but after removing unnecessary rows and columns it has the years as columns and the stat fields as rows: # …

Total answers: 1

Dataframe pivot when indexes are duplicated

Dataframe pivot when indexes are duplicated Question: What Python code will get me from Original DataFrame to Final DataFrame? I am struggling to find any pandas functions for this type of a pivot. Original DataFrame: test1 test2 class value1 0 1 5 type type1 1 1 5 type type3 2 2 6 type type1 3 …

Total answers: 1

loop through a group by, aggregate, and make new columns based on the groups

loop through a group by, aggregate, and make new columns based on the groups Question: I am trying to get by-student columns that count up activities by their progress level. Data looks like STUDENT_ID STUDENT_ACTIVITY_SESSION_ID NODE_NAME ACTIVITY_NAME prog_level FredID gobbledeegook1 Node1 MyActivity1 pass FredID gobbledeegook2 Node1 MyActivity1 pass FredID gobbledeegook3 Node2 MyActivity2 pass JaniceID gobbledeegook4 …

Total answers: 2

Pandas: calculate time difference between different milestones in column

Pandas: calculate time difference between different milestones in column Question: I have a table like this: id tm milestone 00335c06f96a21e4089c49a5da 2023-02-01 18:13:42.307543 A 00335c06f96a21e4089c49a5da 2023-02-01 18:14:42.307543 A 00335c06f96a21e4089c49a5da 2023-02-01 18:15:42.307543 A 00335c06f96a21e4089c49a5da 2023-02-01 18:19:10.307543 B 00335c06f96a21e4089c49a5da 2023-02-01 18:21:05.307543 C 0043545f6b9112c7e471d5cc81 2023-02-02 08:06:42.307543 A 0043545f6b9112c7e471d5cc81 2023-02-02 08:07:42.307543 A 0043545f6b9112c7e471d5cc81 2023-02-02 09:05:42.307543 B 0043545f6b9112c7e471d5cc81 2023-02-02 09:05:42.307543 B …

Total answers: 1

How to pivot without aggregation in python or sql

How to pivot without aggregation in python or sql Question: I want to pivot a table such that each row in the type column is now its own row. Each metric is then a row and the values are the intersection of the metric and type. There are a variable number of types and metrics.Example. …

Total answers: 1

Pandas pivot to explode columns and fill values?

Pandas pivot to explode columns and fill values? Question: I have a Pandas dataframe like so movie, week, sales —–, —-, —— 1, 1, 100 … 1, 52, 200 2, 1, 500, … What I actually want it to look like is movie, week_1, … week_52, 1, 1, 200 2, 1, 500 So what’s effectively …

Total answers: 1

pythonic equivalent of R double pivot and filter

pythonic equivalent of R double pivot and filter Question: A task I come up against reasonable often is something like the following transformation: from: home_team_id away_team_id home_team away_team 1 1 2 Arsenal Tottenham 2 2 3 Tottenham Chelsea to team value 1 Arsenal 1 2 Tottenham 2 3 Tottenham 2 4 Chelsea 3 in my …

Total answers: 1

Python saving customer minimum order date value per customer from multiple values

Python saving customer minimum order date value per customer from multiple values Question: I have df : df = pd.DataFrame({"customer": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"], "order_date": ["12-12-2022", "12-12-2020", "12-12-2019", "12-12-2018", "12-12-2022", "12-12-2022", "12-12-2015", "12-12-2019", "12-12-2017"]}) from which I need the 1st order date (min value) per customer, saved as a new …

Total answers: 1

Pandas special pivot dataframe

Pandas special pivot dataframe Question: Let’s take a sample dataframe : df = pd.DataFrame({"Name": ["Alan","Alan","Kate","Kate","Brian"], "Shop" :["A","B","C","A","B"], "Amount":[4,2,1,3,5]}) Name Shop Amount 0 Alan A 4 1 Alan B 2 2 Kate C 1 3 Kate A 3 4 Brian B 5 First expected output : I would like to create a new dataframe from df …

Total answers: 1

Python Dict pivot keys to columns and values as rows in a dataframe

Python Dict pivot keys to columns and values as rows in a dataframe Question: Evening, I’m new to python and using an open source api to get the longitude and latitudes for postcodes. I have got this far but struggling on how to pivot the dictionary. code: import requests import pandas as pd url = …

Total answers: 1