Home > Blockchain >  replace panda dataframe rows with list comprehension
replace panda dataframe rows with list comprehension

Time:03-25

Hello coding friends :),

I have a panda Dataframe where a column is called "studytime"

My goal was to replace every column in every row where studytime was below the value of 2, with a string that contains "not enough"

And I wanted to use list comprehension within a replacement function!

What would be the best way to do that? My idea was this:

return ["not enough" for x in data["studytime"] if x <= 2]

CodePudding user response:

Use indexing

data.loc[data['studytime'] <= 2, 'studytime'] = 'not enough'
print(data)

# Output
     studytime
0   not enough
1   not enough
2   not enough
3   not enough
4            7
5            8
6   not enough
7            8
8   not enough
9            5
10           9
11  not enough
12           3
13           8
14  not enough

Setup:

np.random.seed(2022)
data = pd.DataFrame({'studytime': np.random.randint(0, 10, 15)})

CodePudding user response:

Use Series.mask

data["studytime"] = data["studytime"].mask(data["studytime"].le(2), "not enough")

Comprehension list is not recommended but:

data["studytime"] = ["not enough" if x <= 2 else x for x in data["studytime"]]

In order to replace all columns do:

data[data["studytime"].le(2)] = "not enough"

Or:

data = data.mask(data["studytime"].le(2), "not enough")
  • Related