Supposed I have a data frame with these rows as the last 8 row.
time a b b d e f
2018-03-04 10:00:00 86.0 194.0 1.084830 1.088466 196.000000 84.333333
2018-03-04 10:30:00 37.0 59.0 1.082257 1.091397 203.000000 87.833333
2018-03-04 11:00:00 65.0 117.0 1.068825 1.091043 220.166667 96.666667
2018-03-04 11:30:00 10.0 9.0 1.070807 1.087203 183.666667 82.333333
2018-03-04 12:00:00 94.0 157.0 1.083382 1.077549 112.833333 61.666667
2018-03-04 12:30:00 66.0 68.0 1.075636 1.077623 100.666667 59.666667
2018-03-04 13:00:00 224.0 607.0 1.152262 1.088861 169.500000 82.666667
2018-03-04 13:30:00 112.0 279.0 1.119430 1.095057 206.166667 95.166667
How do I create a new column "g" using this condition on Pandas: If the row is the last row, value is 100%, If the row is the 2nd last row, value is 95%.. until it reach 70%, else it will be 0?
IIUC, g is not already in df.columns, so we can do:
vals = np.arange(0.7,1,0.05)
df['g'] = 0
df.iloc[-len(vals):, -1] = vals
Looking at the answer above, I thought of not posting this, but anyways -
Assuming you created a dataframe -
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy','Jason', 'Molly', 'Tina', 'Jake', 'Amy','Jason', 'Molly', 'Tina', 'Jake', 'Amy','Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'age': [42, 52, 36, 24, 73, 42, 52, 36, 24, 73, 42, 52, 36, 24, 73, 42, 52, 36, 24, 73],
'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 4, 24, 31, 2, 3],
df = pd.DataFrame(data, columns = ['name', 'age', 'preTestScore', 'postTestScore'])
df
It gives out a dataframe -

Now create a new list that can be added as the new column in our dataframe -
indexList = df.index.tolist()
listToBeInsertedInNewColumn = []
newElement = 100
i = len(indexList)-1
listToBeInsertedInNewColumn.append(str(newElement)+"%")
while i >= 1:
newElement -= 5
if newElement >= 70:
listToBeInsertedInNewColumn.append(str(newElement)+"%")
else:
listToBeInsertedInNewColumn.append("0%")
i -= 1
listToBeInsertedInNewColumn.reverse()
And then finally add this to the dataframe -
df['g'] = list(listToBeInsertedInNewColumn)
This will also give you what you asked for in the question -

It's not as clean as the original answer, but still, an answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With