0
$\begingroup$

I have a large Excel data set which I load as Pandas. In one column I have repeating strings/numbers (the information can be a text and or integers), in another column I have a unique identifier of that row, there is also a column with a due date. Using sort_values I can group the repeating values and sort according to the due dates. But now I would like to add a column containing numbers to get a ranking of the due dates of the first column. example data:

[[Item, Kind, ID, Client, Duedate],[A, 3, 689, XY, 01.04.2019], [A, 3, 689, XY, 02.04.2019],[B, 3, 689, XY, 01.04.2019],[C, 3, 689, XY, 01.04.2019],[A, 3, 689, XY, 01.04.2018],[C, 3, 689, XY, 01.04.2019]

I sort them so that the output is all the A's first, then all the B's, and within that group they are sorted according to priority. But now I want to extend my data set into looking like this:

[Item, Kind, ID, Client, Duedate, **priority**],[A, 3, 689, XY, 01.04.2018, **0**],[A, 3, 689, XY, 01.04.2019, **1**], [A, 3, 689, XY, 02.04.2019**2**]

if tried looping over the values, but it doesn't seem to work, and I could not find anything on Google to help me. Any hint would be much appreciated, I am stuck with this for 10 days now..

$\endgroup$
2
$\begingroup$

I think you are asking how to add a column "priority" to the end of a dataFrame that has been sorted.

Assuming the dataFrame is in the order you like, I would first get the length of the frame:

length = len(df)

Then apply that to a new column in the dataFrame:

df['**priority**'] = ["**"+str(x)+"**" for x in range(length)

I'm still new to posting on here so I don't know how to get the input/output examples working, but that should get you a new column on the end of your frame, in string format, like you pictured above.

$\endgroup$
  • $\begingroup$ Sorry, forgot the close bracket at the very end of the line; please don't forget to add it. $\endgroup$ – Brian Wendel Apr 12 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.