1

I have a file like this:

enter image description here

I have requirement like if the column name 'names' contains '%' the data in 'val' column should be multiplied by 100. Below is the logic I am trying

import pandas as pd 
df = pd.read_csv("pp_sample.csv") 
saved_columns=df['names']
if "%Diff" in saved_columns:
    value=df['val']*100
    print(value)
else:
    print("no values")

When I run this I am getting 'no values' as result, Since I am very new to Python please help me on this.

Anton vBR
  • 18,287
  • 5
  • 40
  • 46
Supriya Rapelli
  • 93
  • 1
  • 5
  • 15
  • 2
    In one line: `df.loc[df['names'].str.contains('%Diff'), 'val'] *= 100` – Anton vBR Nov 13 '18 at 08:15
  • Follow the duplicate for explanation. But in short you use df.loc to select a subset (defined by rows, and cols) and assign a value. **One last thing:** Please do not post images in the future. It makes it hard to run an example on. – Anton vBR Nov 13 '18 at 08:17

0 Answers0