Python Dataframe New Column With Value Based On Value In Other Row
I have another simple question as a follow up from here. Python pandas dataframe sum numbers until they change I want to analyze a serie of numbers which go up and down. So far I h
Solution 1:
Here you are:
import numpy as np
df['LastTB'] = (df.A * df.TB.replace({'':np.nan, 'T':True, 'B':True})).fillna(method="ffill")
df['C'] = df.A / df.LastTB.shift()
and the output is:
A UD ST TB LastTB C
0800NaNNaN12-1-1 B 2.0NaN24112.02.00000035122.02.5000004613T6.03.00000054-1-16.00.66666763-1-2 B 3.00.5000007511T5.01.6666678500T5.01.00000093-1-15.00.600000102-1-25.00.400000
If you are determined to write in one line, you can combine both lines:
df['C'] = df.A / (df.A * df.TB.replace({'':np.nan, 'T':True, 'B':True})).fillna(method="ffill").shift()
Post a Comment for "Python Dataframe New Column With Value Based On Value In Other Row"