I have read in a CSV file with one column. The column contains almost 300 rows of different values. From these values I want to subtract a certain value b=0.157
. These new approx. 300 values should be saved in a new CSV file (array). How can I do this?
This is the csv - file: wearable.csv
right,
0.960,
1.079,
1.019,
1.028,
1.086,
1.042,
.....
.....
.....
and the part of my code, where I read in the csv - file:
wearable = pd.read_csv("wearable.csv")
Now I want to subtract b from all values in the csv - file. So that I end up with new values which are saved in a new CSV file. How can I reach this? Could someone please explain this to me? Thank you.
The final result (new CSV file) should look like this:
wearable_new.csv
right,
0.803,
0.922,
0.862,
0.871,
0.929,
0.885,
.....
.....
.....
Maybe I have to do a for - loop. Maybe someone could explain to me some kind of sample how to do it. Thanks a lot.
EDIT I do not understand why this question was closed. I have already received (see below) an answer of my question before the question was closed. This answer also helped me. I did not need another one.
You can use the broacasting property of numpy
(pandas
is based on numpy
arrays) and simply subtract a constant value from the dataframe.
Edit: adding the part to save to file
import pandas as pd
import numpy as np
b = 0.157
df = pd.DataFrame({'right': np.linspace(0, 1, 5)})
df
right
0 0.00
1 0.25
2 0.50
3 0.75
4 1.00
df - b
right
0 -0.157
1 0.093
2 0.343
3 0.593
4 0.843
(df - b).to_csv('/path/output.csv', index=False, line_terminator = ',\n')