3

I would like to turn some irregular values in my DataFrame to zero.

For example,

import pandas as pd

df1=pd.DataFrame({'product':['a','b','c','d','e','f','g'],'price':[1,2,3,4,5,6,7],'label':['h','i','j','k','L','n','m']})
print(df1)

I would like to change the red values into zero. (There is no regularity)

How can I specify the column/row and change the value?

enter image description here

If you could tell me how to do that, I would be grateful with that very much.

miradulo
  • 28,857
  • 6
  • 80
  • 93
Tom_Hanks
  • 517
  • 1
  • 6
  • 15

4 Answers4

3

You can do any of the following three.

First Approach: using loc[]

df1.loc[1, 'price'] = 0
df1.loc[2, 'price'] = 0
df1.loc[3, 'product'] = 0
df1.loc[5, 'label'] = 0

Second Approach: using set_value()

df1.set_value(1, 'price', 0)
df1.set_value(2, 'price', 0)
df1.set_value(3, 'product', 0)
df1.set_value(5, 'label', 0)

Third Approach:

df1['price'][1] = 0
df1['price'][2] = 0
df1['product'][3] = 0
df1['label'][5] = 0

Third approach is perhaps not the best approach as you will get a warning message. See this stackoverflow post to know in detail about the warning message. But in all cases, you will get the desired output.

Community
  • 1
  • 1
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
2

Below is the general case of how to set or change elements inside dataframes:

dataFrame['keyName'][elementIndex] = newValue

where dataFrame is the variable name where the dataframe is stored, keyName is the name of your key, and elementIndex is the 0-based index id of your element.

Lest say you want to change the value '2' in (1, price) to '5'. This is the code that will do just that:

df1['price'][1] = 5

Hope this helps!

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
0xdesdenova
  • 192
  • 1
  • 8
2

Use set_value:

df1.set_value(1,'price', 0)
print (df1)

Output:

  label  price product
0     h      1       a
1     i      0       b
2     j      3       c
3     k      4       d
4     L      5       e
5     n      6       f
6     m      7       g

Another way is:

df1.loc[1, 'price'] = 0
Serenity
  • 35,289
  • 20
  • 120
  • 115
2

Two cases for you here.

If you don't know where those values are in each column

You could use replace to do a column-row lookup for the values you want to replace, and fix the replacement value as 0.

df1.replace({'label':'n', 'price':[2, 3], 'product': 'd'}, 0)

Demo

>>> df1

  label  price product
0     h      1       a
1     i      2       b
2     j      3       c
3     k      4       d
4     L      5       e
5     n      6       f
6     m      7       g

>>> df1.replace({'label':'n', 'price':[2, 3], 'product': 'd'}, 0) 

  label  price product
0     h      1       a
1     i      0       b
2     j      0       c
3     k      4       0
4     L      5       e
5     0      6       f
6     m      7       g

If you do know where those values are in each column

..then this operation will be much quicker.

Use at[], which is a speedy scalar-based accessor.

to_rep = [(1, 'price'), (2, 'price'), (3, 'product'), (5, 'label')]
for ele in to_rep:
    df1.at[ele] = 0
miradulo
  • 28,857
  • 6
  • 80
  • 93