-1

I have a dataframe with columns that have a bunch of numbers ranging from 1 - 100. I want to split the values so that if a value is between 1 - 10, they are replaced with 0. If a value is between 11 - 20, they are replaced with 1. If the value is between 21 - 30, they are replaced with 2, and so on.

How can I do this with Pandas?

What I've tried:

dating["attr1_1"] = dating["attr1_1"][0:11] = 0
dating["attr1_1"] = dating["attr1_1"][11:21] = 1
sjoelly
  • 147
  • 2
  • 11

3 Answers3

1

You can use the binning process with pd.cut().

import pandas as pd

a = pd.Series(range(1,100))
pd.cut(a, bins=[1,11,21,31,41,51,61,71,81,91,101], labels=False, right=False)
r-beginners
  • 31,170
  • 3
  • 14
  • 32
0

You can do an apply on all columns like this:

df.apply(lambda x: x-1 // 10)

This is call floor division: 2.9 becomes 2, 1.8 becomes 1 etc. Since 30 should become 2 in your case, we substract 1 from the value before we apply the floor division.

JarroVGIT
  • 4,291
  • 1
  • 17
  • 29
-1

you can create a function to perform the operations and passed it as an argument of .apply(). e.g. :

def function_name(rows):
    if row > 0 and row <= 10:
        return 0
    elif othercases ...

for cols in df.columns: # loop over all columns in your dataframe
    df[cols] = df[cols].apply(function_name)