python - 在Python数据框中添加密集排名,其中所有列都在字符串中

标签 python pandas

如何在 pandas 的数据帧中添加排名列,其中数据帧数据类型的所有列都在 string 中。以基于 col1 和 col2 按条件实现以下输出组。尝试创建列 RANK(它将像 sql 一样作为 dnese 等级工作)。

df = pd.DataFrame({
'Col1' : ['A1',"A1","A1","A1","A1","A1","A2","A2","A2","A2","A2","A2","A4","A4","A5","A5","A5"],
'Col2' : ['B1',"B1","B1","B2",'B3',"B4","B1","B1",'B2',"B2","B2","B3","B3","B1","B1","B2","B3"],
'Col3' : ['A101',"A102","A103","A104",'A105',"A106","A107","A108","A109","A110","A111","A112","A113","A114","A115","A116","A117"]
})
df

enter image description here

最佳答案

尝试:

df['RANK'] = df.groupby(['Col1','Col2']).cumcount() + 1

输出:

   Col1 Col2  Col3  RANK
0    A1   B1  A101     1
1    A1   B1  A102     2
2    A1   B1  A103     3
3    A1   B2  A104     1
4    A1   B3  A105     1
5    A1   B4  A106     1
6    A2   B1  A107     1
7    A2   B1  A108     2
8    A2   B2  A109     1
9    A2   B2  A110     2
10   A2   B2  A111     3
11   A2   B3  A112     1
12   A4   B3  A113     1
13   A4   B1  A114     1
14   A5   B1  A115     1
15   A5   B2  A116     1
16   A5   B3  A117     1

关于python - 在Python数据框中添加密集排名,其中所有列都在字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65623384/

相关文章:

python - 名称错误 : name 'RegexValidator' is not defined

python - 尝试使用之前训练的 tf.keras 模型作为预训练,但得到“ValueError : Input 0 of layer dense_3 is incompatible with the laye

python - 如何使用请求下载图像

python - 使用 groupby 对多列进行加权平均,逐列删除 NaN

python - 为什么 Pyarrow 可以读取额外的索引列,而 Pandas dataframe 却不能?

python - 最后五分钟的 Pandas 滚动总和

python - 通过使用 Python Glom 过滤其他属性的列表来获取嵌套属性的值

python - Flask 与事件循环相结合

python - Pandas COUNTIF 每个数据框列值

python-3.x - 刻度标签仅显示在一个子图中