pandas - finding the difference in years for two common values in a column -


I am using pods and I am trying to find the difference in those years when my data is grouped by label And then the teams are done. I have tried to use a group for the problem with which I am working, but I can not get enough of my desired result here, here is the head of my DF (8)

  Team Year Labels Hawks 2001 B Hooks 2004 B Nets 1987 B Nets 1988 Nets 2004 B Nets 2001 Nets 2000 C Hawk 2003 A   

So what makes me say that basically There are two groups that I want - the label and team again, between those years There is need to find the level that would be solved and the results will be different column. Any help would be greatly appreciated. Team Year Labels Inter Hoc 2001 B NAN Hawks 2004 B1 Nets 1987 B NAN Net 1988 A NN Net 2004 B17 Net 2001 a 13 Nets 2000 C NN Hawks 2003 B2

Not sure if label is considered for the last line 'A' or 'b' from your data snippet:

  Hawk 2003 one   

Output expected by you:

  Hawks 2003 b 2   

I think the label is considered 'b', so I I can match with your expected output:

You can use ['team', 'label'] by a group that you can use to calculate the difference of the year, but first [ Sort by 'Team', 'Label', 'Year'] so that the difference of your year is correct:

 In  [8]: df.sort (['Team', 'Labels ',' Year '], inplace = true) [9]: DF out [9]: Team Word labels 0 Hawk 2001 B7 Hawk 2003 B1 Hawk 2004 B3 Net 1988 1 5 Net 2001 2 Net 1987 B4 Nets 2004 b6 nets 2000 c   

Now, group it on ['team', 'labels'] and calculate the difference between the years for each line in the group: <[11] in [10]: df ['difference'] = Df.groupby (['team', 'label']) ['year']. [11] in Diff (1): DF Team Year labels inter-0h 2001 bnn 7 hac 2003 b2 1 hawks 2004 b1 net 1 88 88 a nan 5 net 2001 a 13 2 net 1987 b nan 4 net 2004 b 17 6 Net 2000CNAN

And if for some reason you want to get back to the original order of dataframe, you can do the following:

  In [12]: df.sort_index () Out [12]: Team Year Label Inter 0 Hawk 2001 B Nyan 1 Hawk 2004 B1 Net 1987 B Nyan 3 Net 1988 1 NN 4 Net 2004 B1 7 5 Net 2001 One 13 6 Nets 2000CNAN7Hok 2003b 2    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -