Home > Blockchain >  How to edit all data value given in a dataframe except for the values of a particular index?
How to edit all data value given in a dataframe except for the values of a particular index?

Time:06-16

I have a dataframe consisting of float64 values in it. I have to divide each value by hundred except for the the values of the row of index no. 388. For that I wrote the following code.

Dataset

Preprocessing:

df = pd.read_csv('state_cpi.csv')
d = {'January':1, 'February':2, 'March':3, 'April':4, 'May':5, 'June':6, 'July':7, 'August':8, 'September':9, 'October':10, 'November':11, 'December':12}
df['Month']=df['Name'].map(d)
r = {'Rural':1, 'Urban':2, 'Rural Urban':3}
df['Region_code']=df['Sector'].map(r)
df['Himachal Pradesh'] = df['Himachal Pradesh'].str.replace('--','NaN')
df['Himachal Pradesh'] = df['Himachal Pradesh'].astype('float64')

Extracting the data of use:

data = df.iloc[:,3:-2]

Applying the division on the data dataframe

data[:,:388] = (data[:,:388] / 100).round(2)
data[:,389:] = (data[:,389:] / 100).round(2)

It returned me a dataframe where the data of row no. 388 was also divided by 100. Dataset

CodePudding user response:

As an example, I give the created dataframe. Indices except for 10 are copied into the aaa list. These index numbers are then supplied when querying and 1 is added to each element. The row with index 10 remains unchanged.

df = pd.DataFrame({'a': [1, 23, 4, 5, 7, 7, 8, 10, 9],
                   'b': [1, 2, 3, 4, 5, 6, 7, 8, 9]},
                  index=[1, 2, 5, 7, 8, 9, 10, 11, 12])

aaa = df[df.index != 10].index
df.loc[aaa, :] = df.loc[aaa, :]   1

In your case, the code will be as follows:

aaa = data[data.index != 388].index
data.loc[aaa, :] = (data.loc[aaa, :] / 100).round(2)
  • Related