import pandas as pd
datas1 = [{'A':10, 'B':100}, {'A':11,'B':-210}, {'A':12,'B':120}, {'A':14, 'B':30}]
datas2 = [{'A':25, 'B':90}, {'A':11,'B':-50}, {'A':110,'B':(-15)}, {'A':5, 'B':65}]
datas3 = [{'A':5, 'B':-100}, {'A':19,'B':210}, {'A':12,'B':-300}, {'A':14, 'B':30}]
for i in [datas1, datas2, datas3]:
df = pd.DataFrame(i)
df['CUMSUM'] = (df['B'].cumsum())
if (df['CUMSUM'] < -100).any():
print(df.loc[df['CUMSUM'] < -100, :].head(1))
print ("")
else:
print(df.loc[df['CUMSUM'] >= -100, :].tail(1))
print ("")
使用此程序中获得的结果创建新数据框的代码是什么?
答案 0 :(得分:0)
这会将所有输出聚合到一个数据帧中。我创建了一个字典,从每个结果中为字典分配键值,然后最终连接它们。
import pandas as pd
datas1 = [{'A':10, 'B':100}, {'A':11,'B':-210}, {'A':12,'B':120}, {'A':14, 'B':30}]
datas2 = [{'A':25, 'B':90}, {'A':11,'B':-50}, {'A':110,'B':(-15)}, {'A':5, 'B':65}]
datas3 = [{'A':5, 'B':-100}, {'A':19,'B':210}, {'A':12,'B':-300}, {'A':14, 'B':30}]
results = {}
for idx, i in enumerate([datas1, datas2, datas3]):
df = pd.DataFrame(i)
df['CUMSUM'] = (df['B'].cumsum())
if (df['CUMSUM'] < -100).any():
results[idx] = df.loc[df['CUMSUM'] < -100, :].head(1)
print(results[idx])
print ("")
else:
results[idx] = df.loc[df['CUMSUM'] >= -100, :].tail(1)
print(results[idx])
print ("")
result = pd.concat(list(results.values()), ignore_index=True)