对数据帧行排序

2024-03-29 05:26:53 发布

您现在位置:Python中文网/ 问答频道 /正文

示例输入数据帧

import pandas as pd
df_input = pd.DataFrame([[1.7, 0.2], [0.4, 0.93], [0.05, 0.96], [0.97, 0.68]], columns=["A", "B"])

这个例子有两列,而实际的数据帧有10列。我想按升序对每一行排序,然后将-1分配给前5列,将+1分配给后5列。样本输出如下如下所示:你知道吗

df_output=pd.DataFrame([[1, -1], [-1, 1], [-1, 1], [1, -1]], columns=["A", "B"])

请提出前进的方向。你知道吗


Tags: columns数据import示例dataframepandasdfinput
3条回答

你想要^{}

np.argsort(df_input, axis=1).replace(0, -1)

   A  B
0  1 -1
1 -1  1
2 -1  1
3  1 -1

概括为N行:

v = np.where(np.argsort(df_input) >= df.shape[1] // 2, 1, -1)    
df_output =  pd.DataFrame(v)

print(df)
    0   1   2   3   4   5   6   7   8   9
0  49  80  80  27  15  13  52  50  48  69
1  51  24  55  73  81  55  32  67  19  14
2  67   2  29  19  14  89  54  83  22  64
3  24  55  87  94  22  61  74  26  37   8

v = np.where(np.argsort(df_input) >= df.shape[1] // 2, 1, -1)    
df_output =  pd.DataFrame(v)

print(df_output)
   0  1  2  3  4  5  6  7  8  9
0  1 -1 -1  1 -1  1  1  1 -1 -1
1  1  1 -1  1 -1 -1  1  1 -1 -1
2 -1 -1 -1  1 -1  1  1 -1  1  1
3  1 -1 -1  1  1 -1  1  1 -1 -1

^{}^{}一起使用:

np.random.seed(111)

df_input = pd.DataFrame(np.random.randint(10, size=(10, 10)), columns=list('abcdefghij'))
print (df_input)
   a  b  c  d  e  f  g  h  i  j
0  6  8  3  6  6  7  1  8  3  4
1  5  4  3  7  8  7  0  1  7  2
2  5  9  0  5  5  1  9  6  2  1
3  6  0  1  7  0  1  5  9  0  1
4  7  6  6  5  4  9  0  3  8  0
5  2  6  9  7  4  2  9  5  7  9
6  8  8  4  2  5  0  7  0  8  2
7  7  9  0  8  0  2  0  5  8  1
8  7  1  3  7  0  2  0  9  9  3
9  2  2  6  1  9  8  6  0  2  6

arr = np.where(np.argsort(df_input, axis=1) < 5 , -1, 1)

df_output = pd.DataFrame(arr, columns=df_input.columns)
print (df_output)
   a  b  c  d  e  f  g  h  i  j
0  1 -1  1  1 -1 -1 -1  1 -1  1
1  1  1  1 -1 -1 -1 -1  1  1 -1
2 -1  1  1  1 -1 -1 -1  1 -1  1
3 -1 -1  1 -1  1  1  1 -1 -1  1
4  1  1  1 -1 -1 -1 -1 -1  1  1
5 -1  1 -1  1 -1 -1  1 -1  1  1
6  1  1 -1  1 -1 -1  1 -1 -1  1
7 -1 -1  1  1  1  1 -1 -1  1 -1
8 -1  1 -1  1 -1  1 -1 -1  1  1
9  1 -1 -1 -1  1 -1  1  1  1 -1

您可以^{},然后通过^{}有条件地赋值:

df[:] = np.where(df.rank(axis=1) > df.shape[1] / 2, 1, -1)

print(df)

   A  B
0  1 -1
1 -1  1
2 -1  1
3  1 -1

注意:这假设重复的值总是得到相同的秩。你知道吗

相关问题 更多 >