Pandas 浮动到数据帧
我有一个for循环,它计算每年的股票市值:Pandas 浮动到数据帧,pandas,for-loop,Pandas,For Loop,我有一个for循环,它计算每年的股票市值: close_dec = close.resample("1y").ffill() for element in close_dec: x=0 for i in close_dec[element]: market_cap = shares[x] * i x+=1 print('Market Cap of', element,"
close_dec = close.resample("1y").ffill()
for element in close_dec:
x=0
for i in close_dec[element]:
market_cap = shares[x] * i
x+=1
print('Market Cap of', element,"{:,.2f}".format(market_cap))
但当我尝试将输出放入数据框时,它表示市值是浮动的,并且它是输出中的最后一个数字。
如何将其转换为列表或数据帧,以便显示所有数字 输出是否需要是数据帧,是否需要使用熊猫 我建议使用一种新的方法 在循环之前实例化一个空数组,如
market_caps = np.array([])
然后在循环中你可以做什么
market_cap = shares[x] * i
x+=1
print('Market Cap of', element,"{:,.2f}".format(market_cap))
market_caps = np.append(market_caps, market_cap)
如果您必须使用Pandas,我建议您在数据帧上使用。
代码类似,但您将使用Pandas系列。输出是否需要是数据帧,以及是否需要使用Pandas 我建议使用一种新的方法 在循环之前实例化一个空数组,如
market_caps = np.array([])
然后在循环中你可以做什么
market_cap = shares[x] * i
x+=1
print('Market Cap of', element,"{:,.2f}".format(market_cap))
market_caps = np.append(market_caps, market_cap)
如果您必须使用Pandas,我建议您在数据帧上使用。
代码类似,但您将使用Pandas系列