简体   繁体   English

pandas/python:创建一个计算类别的数字分类变量

[英]pandas/python: creating a numerical categorical variable that counts the categories

I am trying to build a column in a pandas DF that is counting the category CHANGES of a categorical variable in a "rolling" way.我正在尝试在 pandas DF 中构建一个列,该列以“滚动”方式计算分类变量的类别变化。 What I keep on finding in stackoverflow is a number of rolling counts, which is exactly the opposite of what I am looking for.我在 stackoverflow 中不断发现的是一些滚动计数,这与我正在寻找的正好相反。 I am looking for a column that runs through an alphabetically sorted categorical column and adds an increment of 1 every time the category changes but gets dragged unchanged otherwise.我正在寻找一个列,该列贯穿按字母顺序排序的分类列,并且每次类别更改时都会增加 1,否则会被拖拽而保持不变。 So if I have the variable named 'cat_var' in the example below, I need to programmatically create the column 'category_counter_var' which I manually created in the example below.因此,如果我在下面的示例中有名为“cat_var”的变量,我需要以编程方式创建我在下面的示例中手动创建的列“category_counter_var”。 Can someone help?有人可以帮忙吗?

import pandas as pd

df = pd.DataFrame({'cat_var':['Q1','Q1','Q1','Q2','Q2','Q3','Q4','Q4','Q4','Q4']
                   ,'category_counter_var':[1,1,1,2,2,3,4,4,4,4]})

在此处输入图像描述

Use:利用:

df['new'] = df['cat_var'].ne(df['cat_var'].shift()).cumsum()
print(df)

# Output
  cat_var  category_counter_var  new
0      Q1                     1    1
1      Q1                     1    1
2      Q1                     1    1
3      Q2                     2    2
4      Q2                     2    2
5      Q3                     3    3
6      Q4                     4    4
7      Q4                     4    4
8      Q4                     4    4
9      Q4                     4    4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取分类变量的类别列表(Python Pandas) - Get a list of categories of categorical variable (Python Pandas) 将带有 % 符号的分类变量转换为数值变量 Python Pandas - Converting Categorical Variable with % Sign to Numerical Variable Python Pandas 创建带有分类数据计数的pandas DataFrame - Creating a pandas DataFrame with counts of categorical data pandas 数据帧中的单独数字和分类变量 - separate numerical and categorical variable in pandas datframe 我想在 Python 中将分类变量转换为数值 - I want to convert the categorical variable to numerical in Python 自动计算python pandas中分类变量每列有多少个类别 - Count how many categories each column of categorical variable in python pandas automatically 如何在不增加数据大小的情况下将大熊猫中的分类变量转换为数值? - How to convert categorical variable to numerical in pandas without increasing size of data? 在 Python 中创建虚拟变量的分类变量 - Creating categorical variable of dummies in Python 查看 python 中分类变量和数值变量之间相关性的最佳方法, - Best way to see correlation between a categorical variable and numerical variable in python, 如何在 python pandas 的 for 循环中将分类数据转换为数值数据 - how to convert categorical data to numerical data in for loop in python pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM