Created
December 17, 2012 22:54
-
-
Save lebedov/4323181 to your computer and use it in GitHub Desktop.
Demo of how to find the order of several entries in a row of numerical data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Demo of how to find the order of several entries in a row of numerical data. | |
""" | |
import numpy as np | |
import pandas | |
df = pandas.DataFrame(np.random.rand(5, 3), columns=['a', 'b', 'c']) | |
def find_rank(x): | |
""" | |
Return ordering of given list. | |
""" | |
N = len(x) | |
x_sorted = sorted(x) | |
result = [] | |
for i in xrange(N): | |
result.append(x_sorted.index(x[i])) | |
return result | |
def find_rank_rev(x): | |
""" | |
Return reverse ordering of given list. | |
""" | |
N = len(x) | |
x_sorted = sorted(x, reverse=True) | |
result = [] | |
for i in xrange(N): | |
result.append(x_sorted.index(x[i])) | |
return result | |
# Find ordering of each row: | |
df_rank = df.apply(find_rank, 1) | |
df_rank_rev = df.apply(find_rank_rev, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment