Python 如何为数据帧中最后几行的列定义条件?

Python 如何为数据帧中最后几行的列定义条件?,python,pandas,Python,Pandas,假设我有一个数据框,其中这些行是最后8行 时间a 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.091

假设我有一个数据框,其中这些行是最后8行

时间a 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
如何在熊猫上使用此条件创建新列“g”: 如果该行是最后一行,则值为100%,
如果该行是最后第二行,则值为95%。。直到它达到70%,否则它将是0

IIUC,
g
不在
df.列中,因此我们可以执行以下操作:

vals = np.arange(0.7,1,0.05)
df['g'] = 0
df.iloc[-len(vals):, -1] = vals

看到上面的答案,我想不发这个,但无论如何-

假设您创建了一个数据帧-

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
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()
df['g'] = list(listToBeInsertedInNewColumn)
它发出一个数据帧-

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
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()
df['g'] = list(listToBeInsertedInNewColumn)

现在创建一个新列表,该列表可以添加为数据框中的新列-

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
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()
df['g'] = list(listToBeInsertedInNewColumn)
然后最后将其添加到数据帧中-

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
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()
df['g'] = list(listToBeInsertedInNewColumn)
这也将为您提供您在问题中所要求的-


它不像原来的答案那么清晰,但仍然是一个答案。

你的百分比是基于什么?如果f列是100%吗?@vlemaistre 100%将引用数据帧中的最后一行,95%引用最后第二行,等等,并且不基于其他列中的其他值。很抱歉返回,但您能向我解释一下:data.iloc[-len(vals):,-1]=vals的含义吗?