简体   繁体   中英

Is there a way to iterate over a column in Pandas to find matching index values from another dataframe?

I have two dataframes, df_diff and df_three. For each column of df_three, it contains the index values of three largest values from each column of df_diff. For example, let's say df_diff looks like this:

     A    B    C
  0  4    7    8
  1  5    5    7
  2  8    2    1
  3  10   3    4
  4  1    12   3

Using

df_three = df_diff.apply(lambda s: pd.Series(s.nlargest(3).index))

df_three would look like this:

     A    B    C
  0  3    4    0
  1  2    0    1
  2  1    1    3

How could I match the index values in df_three to the column values of df_diff?
In other words, how could I get df_three to look like this:

     A     B    C
  0  10   12    8
  1   8    7    7
  2   5    5    4

Am I making this problem too complicated? Would there be an easier way?
Any help is appreciated!

def top_3(s, top_values):
    res = s.sort_values(ascending=False)[:top_values] 
    res.index = range(top_values)
    return res

res = df.apply(lambda x: top_3(x, 3))
print(res)

Use numpy.sort with dataframe values:

n=3
arr = df.copy().to_numpy()

df_three = pd.DataFrame(np.sort(arr, 0)[::-1][:n], columns=df.columns)

print(df_three)
    A   B  C
0  10  12  8
1   8   7  7
2   5   5  4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM