简体   繁体   English

创建基于另一个列值增加计数的 Dataframe 列

[英]Create Dataframe column that increases count based on another columns value

I need to add a column to my dataframe that will add a number each time a value in another column surpasses a limit.我需要向我的数据框中添加一列,每当另一列中的值超过限制时,该列将添加一个数字。 Example below:下面的例子:

Original DF:原始DF:

ColA   ColB
 4      1.4
 10     0.5
 1      2.3
 3      12.2
 8.8    8.5
 2      5.2
 0.6    0.33
 9      3
 4      144
 33     8

Desired DF: Where value in ColB surpasses 10, ColC count = count +1期望的 DF:ColB 中的值超过 10,ColC 计数 = 计数 +1

ColA   ColB     ColC
 4      1.4       1
 10     0.5       1
 1      2.3       1
 3      12.2      2
 8.8    8.5       2
 2      5.2       2
 0.6    0.33      2
 9      3         2
 4      144       3
 33     8         3

thank you for the help!感谢您的帮助!

df['ColC'] = (df['ColB'] > 10).cumsum() + 1
print(df)

Prints:印刷:

   ColA    ColB  ColC
0   4.0    1.40     1
1  10.0    0.50     1
2   1.0    2.30     1
3   3.0   12.20     2
4   8.8    8.50     2
5   2.0    5.20     2
6   0.6    0.33     2
7   9.0    3.00     2
8   4.0  144.00     3
9  33.0    8.00     3

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

相关问题 Python数据框:基于另一列创建列 - Python Dataframe: Create columns based on another column 根据另一列中的值组合数据框的列 - Combining columns of dataframe based on value in another column 根据另一个 dataframe 中的列值创建 dataframe 列 - Create a dataframe column based on values that are columns in another dataframe 根据条件将一个 dataframe 中的列值设置为另一个 dataframe 列 - Setting value of columns in one dataframe to another dataframe column based on condition Pandas 根据来自另一个 dataframe 的计数和条件创建新列 - Pandas Create new column based on a count and a condition from another dataframe 根据列和另一个字典中的索引列表创建新的 dataframe 列 - Create new dataframe columns based on lists of indices in a column and another dictionary 根据来自另一个数据帧的多列条件创建多列 - Create multiple columns based on multiple column conditions from another dataframe 根据熊猫数据框中另一列的最后一个值填充列 - Fill columns based on the last value of another column in a pandas dataframe 如何根据另一个列值对两个数据框列求和 - How to sum two dataframe columns based on another column value 如何将一列添加到基于另一个列值的数据框中? - How can I add a column to a dataframe that is based on another columns value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM