gpt4 book ai didi

python - 在 pandas DataFrame 中添加元素很困难

转载 作者:行者123 更新时间:2023-11-30 22:40:12 25 4
gpt4 key购买 nike

我目前在添加以下 DataFrame 的行时遇到困难,该 DataFrame 是我为六家公司股票的返回构建的:

def importdata(data):

returns=pd.read_excel(data) # Imports the data from Excel
returns_with_dates=returns.set_index('Dates') # Sets the Dates as the df index

return returns_with_dates

输出:

Out[345]: 
Company 1 Company 2 Company 3 Company 4 Company 5 Company 6
Dates
1997-01-02 31.087620 3.094705 24.058686 31.694404 37.162890 13.462241
1997-01-03 31.896592 3.109631 22.423629 32.064378 37.537013 13.511706
1997-01-06 31.723241 3.184358 18.803148 32.681000 37.038183 13.684925
1997-01-07 31.781024 3.199380 19.503886 33.544272 37.038183 13.660193
1997-01-08 31.607673 3.169431 19.387096 32.927650 37.537013 13.585995
1997-01-09 31.492106 3.199380 19.737465 33.420948 37.038183 13.759214
1997-01-10 32.589996 3.184358 19.270307 34.284219 37.661721 13.858235
1997-01-13 32.416645 3.199380 19.153517 35.147491 38.035844 13.660193
1997-01-14 32.301077 3.184358 19.503886 35.517465 39.407629 13.783946
1997-01-15 32.127726 3.199380 19.387096 35.887438 38.409967 13.759214
1997-01-16 32.532212 3.229232 19.737465 36.257412 39.282921 13.635460
1997-01-17 33.167833 3.259180 20.087835 37.490657 39.033505 13.858235
1997-01-20 33.456751 3.229232 20.438204 35.640789 39.657044 14.377892
1997-01-21 33.225616 3.244158 20.671783 36.010763 40.779413 14.179940
1997-01-22 33.110049 3.289033 21.489312 36.010763 40.654705 14.254138
1997-01-23 32.705563 3.199380 20.905363 35.394140 40.904121 14.229405
1997-01-24 32.127726 3.139579 20.204624 35.764114 40.405290 13.957165
1997-01-27 32.127726 3.094705 20.204624 35.270816 40.779413 13.882968
1997-01-28 31.781024 3.079778 20.788573 34.407544 41.153536 13.684925
1997-01-29 32.185510 3.094705 21.138942 34.654193 41.278244 13.858235
1997-01-30 32.647779 3.094705 21.022153 34.407544 41.652367 13.981898
1997-01-31 32.532212 3.064757 20.204624 34.037570 42.275905 13.858235

无数个小时以来,我一直尝试以这样的方式对它们进行总结:将 1997-01-02 到 1997-01-08、1997-01-09 到 1997-01-15 等的行相加,因此将前五行相加,然后将后面的五行相加。此外,我寻求将日期保留为第 5 个元素的索引,因此在将 1997-01-02 到 1997-01-08 的元素相加的情况下,我寻求保留 1997-01-08 作为对应的索引到汇总的元素。值得一提的是,我一直使用五行加法作为示例,但理想情况下,我寻求将每 n 行相加,然后将接下来的 n 行相加,同时以与前面所述相同的方式维护日期。我已经找到了一种方法(如下面的代码所示)以数组形式进行操作,但在这种情况下我无法保留日期。

returns=pd.read_excel(data) # Imports the data from Excel
returns_with_dates=returns.set_index('Dates') # Sets the Dates as the df index

returns_mat=returns_with_dates.as_matrix()
ndays=int(len(returns_mat)/n) # Number of "ndays" in our time-period

nday_returns=np.empty((ndays,min(np.shape(returns_mat)))) # Creates an empty array to fill
# and accommodate the n-day log-returns

for i in range(1,asset_number+1):
for j in range(1,ndays+1):
nday_returns[j-1,i-1]=np.sum(returns_mat[(n*j)-n:n*j,i-1])

return nday_returns

有什么方法可以做到这一点,但在 DataFrame 上下文中,同时按照我之前所说的方式维护日期?我已经尝试这样做很长时间了,但没有取得任何成功,这真的让我压力很大!出于某种原因,每个人都认为 Pandas 非常有用且易于使用,但我却恰恰相反。任何形式的帮助将非常感激。提前致谢。

最佳答案

分组依据

df.groupby(np.arange(len(df)) // 5).sum()

enter image description here

<小时/>

根据要求包含日期索引

g = np.arange(len(df)) // 5
i = df.index.to_series().groupby(g).last()
df.groupby(g).sum().set_index(i)

enter image description here

关于python - 在 pandas DataFrame 中添加元素很困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42914040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com