3

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?

2
  • What do you base your percentage on ? Is it 100% if column f ? Commented Jul 2, 2019 at 15:54
  • @vlemaistre 100% will refer to the last row in the data frame, 95% to the 2nd last row, etc. and not based on other value in other column. Commented Jul 2, 2019 at 15:56

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry to get back but can you explain to me what does: data.iloc[-len(vals):, -1] = vals mean?
1

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 -

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 -

New Dataframe

It's not as clean as the original answer, but still, an answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.