打印DataFrame的特定行

时间:2018-01-25 16:10:07

标签: python pandas

我有这个df:

import pandas as pd
datas = [{'A':10, 'B':100}, {'A':11,'B':(-210)}, {'A':12,'B':120}, {"A":14, "B":30}]
df = pd.DataFrame(datas)
df ["CUMSUM"] = (df["B"].cumsum())

我正在尝试获得以下输出:

1)如果列“CUMSUM”具有低于(-100)的任何值:仅打印CUMSUM <1的第一行。 (-100),并停止运行。

2)如果“CUMSUM”列没有任何低于(-100)的值:只打印最后一行,然后停止运行。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:2)

这可能是你想要的:

import pandas as pd

datas = [{'A':10, 'B':100}, {'A':11,'B':(-210)}, {'A':12,'B':120}, {'A':14, 'B':30}]
df = pd.DataFrame(datas)
df['CUMSUM'] = (df['B'].cumsum())

if (df['CUMSUM'] < -100).any():

# 1) If the column "CUMSUM" has any value lower than (-100):
# print just the first row in which CUMSUM < (-100), and stop running.

    print(df.loc[df['CUMSUM'] < -100, :].head(1))

else:

# 2) If the column "CUMSUM" has not any value lower than (-100):
# print just the last row, and stop running.

    print(df.loc[df['CUMSUM'] >= -100, :].tail(1))