I have a pandas dataframe df:
import pandas as pd
import numpy as np
data = {'A':[250,100,400,np.nan,300]}
df = pd.DataFrame(data)
print(df)
A
0 250.0
1 100.0
2 400.0
3 NaN
4 300.0
I want to transform this datafarme (DF) based on values in the list (values).
values = [0,200,400,600]
In df, first number 250. It is between 200 and 400 in list values
, such that (|200-250|)/(400-200) = 0.25 and (400-250)/(400-200)=0.75,respectively. If data is missing (np.nan) then row must be filled with 0. I want to convert this respresent this dataframe in this manner.
Desired dataframe:
0 200 400 600
0 0.0 0.25 0.75 0.0
1 0.5 0.50 0.00 0.0
2 0.0 0.00 1.00 0.0
3 0.0 0.00 0.00 0.0
4 0.0 0.50 0.50 0.0
2
have a1
in a different column?