0

I have a Pandas dataframe contains some columns. Each columns have some differents values. See the image.

enter image description here

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.

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:

enter image description here

  • What have you tried, and what went wrong with your attempts? For example, numpy.where, df.map, list comprehension...? Your question should include your sample input and expected output as text, not as an image or link, as well as code for what you've tried based on your own research, to make a [mcve] so that we can better understand how to help – G. Anderson Jun 23 '22 at 23:47
  • Could you provide the expected output as well as the input in a [text format](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Edit your post to contain this information. – BrokenBenchmark Jun 23 '22 at 23:47
  • Thank you so much for aswer me @G.Anderson, I have no idea how can I do what I need! –  Jun 23 '22 at 23:53
  • @BrokenBenchmark, I will change my question and put an example of output... Thank you so much for answer me! –  Jun 23 '22 at 23:55

1 Answers1

2

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
Chris Seeling
  • 606
  • 4
  • 11
  • Thank you so much Chris Seeling. This function clip() help me a lot! –  Jun 26 '22 at 16:56