Ted Petrou
1 min readDec 14, 2017

--

This is a good question. Let’s address how you can do this correctly in pandas. Your code is highly non-idiomatic. First, there is no need to do a groupby to sort the values by different groups. You can simply make one call to sort_values to sort by each group like this: df.sort_values(['ID', 'Date']). This will sort by ID first and then by Date. There is no need for a groupby.

Next, you almost never want to use inplace=True and especially not while iterating through the DataFrame like you are doing. The SettingWithCopy warning is getting triggered because each group during your iteration is a subset of the data and you are modifying that subset.

In conclusion, don’t ever sort values like that. Use one call to sort_values by passing all the grouping variable first to a list as an argument.

--

--

Ted Petrou
Ted Petrou

Written by Ted Petrou

Author of Master Data Analysis with Python and Founder of Dunder Data

Responses (1)