Home > OS >  Changing the values in a column using python
Changing the values in a column using python

Time:11-27

import pandas as pd
import numpy as np
data_A=pd.read_csv('D:/data_A.csv')

data_A has column named power. enter image description here

powercolumn only has 0 and 1 and dtype is int64. I want to make sure that there are only 0 and 1 in column power. So, if there are other numbers except 0 and 1 in column power, I want to make the values 0. How can I do?

CodePudding user response:

You can use DataFrame.loc to conditionally access a group of rows and columns.

>>> import pandas as pd
>>>
>>> df = pd.DataFrame({"power": [1, 0, 1, 2, 5, 6, 0, 1]})
>>> df
   power
0      1
1      0
2      1
3      2
4      5
5      6
6      0
7      1
>>> df.loc[~(df["power"].isin([1, 0])), "power"] = 0
>>> df
   power
0      1
1      0
2      1
3      0
4      0
5      0
6      0
7      1

The condition ~(df["power"].isin([1, 0])) returns a Boolean Series which can be use to select the rows that have 'power' not equal to 1 or 0

CodePudding user response:

Try this:

import pandas as pd

# example df

p = [1, 0, 3, 4, 's']

data_A = pd.DataFrame(p, columns=['power'])

def convert_row(row):
    if row == 1 or row == 0:
        return row
    else:
        return 0

data_A['power'] = data_A['power'].apply(convert_row)

print(data_A)

CodePudding user response:

You could also use list comprehension if your dataframe is small.

data_A.power = [x if x == 1 else 0 for x in data_A.power]

Or numpy for a longer column (this solution assumes you don't have negative values)

import numpy as np
power_np = np.array(data_A.power)
power_np[power_np > 1] = 0
data_A.power = power_np
  • Related