Skip to content
Advertisement

Similar to pivot table in Python

Here is a dataframe data_1.

JavaScript

I want to make this data_1 as follows:

JavaScript

I tried pivot_table ,but the output is not the same as I expected. Moreover, I need to save the data_1 as csv file, but the there are no columns id and date in the csv file that I made. Is there any method to change the data_1 as my expected output?

Advertisement

Answer

Because you want the duplicates still in your pivot table, this was an interesting question. My solution is to make a pivot table for the non-duplicates and the duplicates and then concatenate them together.

JavaScript

Output:

JavaScript

Then to a csv file use data_1.to_csv(file_path, index=False)

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