简体   繁体   English

如何将熊猫数据框列中的所有值聚合为 2 个值

[英]How to aggregate all values in a pandas dataframe columns in 2 values

I have a Pandas dataframe contains some columns.我有一个 Pandas 数据框包含一些列。 Each columns have some differents values.每列都有一些不同的值。 See the image.看图片。

在此处输入图像描述

In col1 I have that the value 1 is more frequent than others, so, I need to transform this column to have values 1 and more then 1.在 col1 中,值 1 比其他值更频繁,因此,我需要将此列转换为值 1 和大于 1。

How can I do that?我怎样才能做到这一点?

My goals here is transforme this column in a categorical column but I have no idea how can I do that.我的目标是将此列转换为分类列,但我不知道该怎么做。

The output expected is something like the next image:预期的输出类似于下一张图片:

在此处输入图像描述

Try clip function on column:在列上尝试剪辑功能:

df["col1"].clip(upper=2)

0     1
1     2
2     2
3     2
4     1
5     2
6     2
7     1
8     1
9     1
10    1
11    2
12    1

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

相关问题 计算多列熊猫数据框中的聚合值 - Calculating aggregate values in a pandas dataframe with multiple columns Pandas:如何删除 Pandas 数据框中所有列的前导缺失值? - Pandas: How to drop leading missing values for all columns in a pandas dataframe? 如何聚合然后扩展熊猫数据框中的频率值 - How to aggregate and then expand frequency values in pandas dataframe 如何聚合 DataFrame 中的 3 列以在 Python Pandas 中的单独列中计算和分配值? - How to aggregate 3 columns in DataFrame to have count and distribution of values in separated columns in Python Pandas? 如何在Pandas数据框的所有列中广播和分配一系列值? - How to broadcast and assign a series of values across all columns in a Pandas dataframe? 将同名pandas数据框列的值聚合到单列 - Aggregate values of same name pandas dataframe columns to single column 按熊猫数据框的索引对所有列的值进行分组 - Grouping the values of all columns by index of a pandas dataframe select pandas dataframe 中所有列的值范围 - select range of values for all columns in pandas dataframe 分组并聚合 pandas dataframe 中的值 - Group by and aggregate the values in pandas dataframe 如何聚合 DataFrame 并根据 Python Pandas 中两列中的值删除重复项? - How to aggregate DataFrame and drop duplicates based on values in two columns in Python Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM