Skip to content
Advertisement

How to loop through elements from a python pandas dataframe to a new nested dictionary?

I am currently using pandas library to read data from a CSV file. The data includes a “data” column which consists of 1’s and 0’s, and a “published_at” column which has unique time and date stamps (I have converted it to become the index of the dataframe). Click here to see picture of the Dataframe from CSV (I deleted the core_id data as it is irrelevant).

In the data, a “1” means yes and “0” means no. I would like to analyze the data by looping through the dataframe from a certain start date to an end date (i.e. 2020-11-26 to 2020-11-27) and count how many times “1” (yes_data) occurred, and how many times “0” (no_data) occurred in each day. And from there, I would like to create a new CSV file or dataframe that contains that data so I can analyze it from there.

The way I tried to approach this is by creating a nested dictionary and trying to populate it by looping through the main dataframe and counting how many times “yes” and “no” occurred per day.

I would like to end up with a dictionary (or dataframe, csv file, whatever..) that has 3 columns: date (i.e. 2020-11-26), “yes” count, and “no” count.

Below is the code I came up with:

JavaScript

However the code does not work (I keep getting errors everywhere..). How can I fix it to do what I’m trying to do? Any help is appreciated. Thank you!

P.S. I’m fairly new to Python, trying my best here!

Advertisement

Answer

Hope you’re doing well. This snippet will help you do the job!

JavaScript

Keep in mind that df is your data frame and row[‘data’] means the 0 or 1. So if you have different names, change it. At the end of this code, you’re gonna have a dictionary with the structure as mentions in the following.

JavaScript

Have a good day

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement