【问题标题】:Create a dataframe with tickers as rows and a for loop as columns创建一个数据框,将代码作为行,将 for 循环作为列
【发布时间】:2021-09-22 09:11:12
【问题描述】:

我有一个数据框 close 包含一些股票的收盘价(预先进行一些计算),我想创建一个数据框(带有空条目或随机数),这样行名就是 @ 的代码987654322@ 和列名从 10 到 300,步长为 10。即。 10,20,30,40,50...

我想创建这个 df 以便使用 for 循环来填写所有条目。

我的dfclose如下:

                 Close                                                    \
ticker            AAPL        AMD        BIDU        GOOGL          IXIC   
Date                                                                       
2011-06-01   12.339643   8.370000  132.470001   263.063049   2769.189941   
2011-06-02   12.360714   8.240000  138.490005   264.294281   2773.310059   
2011-06-03   12.265714   7.970000  133.210007   261.801788   2732.780029   
2011-06-06   12.072857   7.800000  126.970001   260.790802   2702.560059   
2011-06-07   11.858571   7.710000  124.820000   259.774780   2701.560059 
......  

我尝试检查我是否首先正确创建了这个数据框,如下所示:

rows = close.iloc[0]
columns = [[i] for i in range(10,300,10)]
print(pd.DataFrame(rows, columns))

但我得到的是:


2011-06-01
10  20  30  40  50  60  70  80  90  100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 NaN

在这之后,我会使用类似的东西

percent = pd.DataFrame(rows, columns)
for i in range(10, 300, 10):
    myerror = myfunction(close, i) # myfunction is a function defined beforehand
    extreme = myerror > 0.1 
    percent.iloc[:,i] = extreme.mean()

具体来说,对于i=10,我的extreme.mean() 是这样的:

                             ticker
Absolute Error (Volatility)  AAPL      0.420
                             AMD       0.724
                             BIDU      0.552
                             GOOGL     0.316
                             IXIC      0.176
                             MSFT      0.320
                             NDXT      0.228
                             NVDA      0.552
                             NXPI      0.476
                             QCOM      0.468
                             SWKS      0.560
                             TXN       0.332
dtype: float64

但是如果我尝试这种方式,我得到了:

IndexError: iloc cannot enlarge its target object

我应该如何首先创建这个 df?还是我什至需要先创建这个 df?

【问题讨论】:

    标签: python pandas dataframe function for-loop


    【解决方案1】:

    以下是我的处理方法:

    from io import StringIO
    import numpy as np
    df = pd.read_csv(StringIO("""ticker_Date            AAPL        AMD        BIDU        GOOGL          IXIC   
    2011-06-01   12.339643   8.370000  132.470001   263.063049   2769.189941   
    2011-06-02   12.360714   8.240000  138.490005   264.294281   2773.310059   
    2011-06-03   12.265714   7.970000  133.210007   261.801788   2732.780029   
    2011-06-06   12.072857   7.800000  126.970001   260.790802   2702.560059   
    2011-06-07   11.858571   7.710000  124.820000   259.774780   2701.560059 """), sep="\s+", index_col=0)
    
    col_names = [f"col_{i}" for i in range(10, 300, 10)]
    # generate random data
    data = np.random.random((df.shape[1], len(col_names)))
    # create dataframe
    df = pd.DataFrame(data, columns=col_names, index=df.columns.values)
    df.head()
    

    这将生成:

            col_10  col_20  col_30  col_40  col_50  col_60  col_70  col_80  col_90  col_100 ... col_200 col_210 col_220 col_230 col_240 col_250 col_260 col_270 col_280 col_290
    AAPL    0.758983    0.990241    0.804344    0.143388    0.987025    0.402098    0.814308    0.302948    0.551587    0.107503    ... 0.270523    0.813130    0.354939    0.594897    0.711924    0.574312    0.124053    0.586718    0.182854    0.430028
    AMD     0.280330    0.540498    0.958757    0.779778    0.988756    0.877748    0.083683    0.935331    0.601838    0.998863    ... 0.426469    0.459916    0.458180    0.047625    0.234591    0.831229    0.975838    0.277486    0.663604    0.773614
    BIDU    0.488226    0.792466    0.488340    0.639612    0.829161    0.459805    0.619539    0.614297    0.337481    0.009500    ... 0.049147    0.452581    0.230441    0.943240    0.587269    0.703462    0.528252    0.099104    0.510057    0.151219
    GOOGL   0.332762    0.135621    0.653414    0.955116    0.341629    0.213716    0.308320    0.982095    0.762138    0.532052    ... 0.095432    0.908001    0.077070    0.413706    0.036768    0.481697    0.092373    0.016260    0.394339    0.042559
    IXIC    0.358842    0.653332    0.994692    0.863552    0.307594    0.269833    0.972357    0.520336    0.124850    0.907647    ... 0.189050    0.664955    0.167708    0.333537    0.295740    0.093228    0.762875    0.779000    0.316752    0.687238
    

    【讨论】:

    • 谢谢。有用。但我事先对 df close 做了一些计算。当我阅读文件时,我使用了pd.concat([read_file(f) for f in glob('*.csv')]).set_index(['Date','ticker'])[['Close']].unstack()。现在,如果我在没有index=df.columns.values 的情况下使用您的代码,除了我的行名是0,1,2,3,4 之外,我得到了您生成的内容。请问我应该如何更改使用代码的行名?
    • 我认为您将 index 称为行名。您可以只使用df.index=val,其中valiterable,例如list,具有所需的index 值。
    • 你的意思是我的closeindex 吗?但我的close.indexDate..
    猜你喜欢
    • 2021-10-08
    • 2020-05-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多