简体   繁体   English

如何将这些日期附加到我的数据框中?

[英]How can I append these dates to my dataframe?

I have the dates seperated from the original creation of the df due to the fact that it enumerates the entire date list to each row.由于它将整个日期列表枚举到每一行,因此我将日期与 df 的原始创建分开。 The number dates I have match the number of rows I have in the enumerated df.我拥有的日期数与我在枚举的 df 中的行数相匹配。 Hope you can help, thx!希望能帮到你,谢谢!

In:在:

daily_shares_df = pd.DataFrame(columns = ['Date', 'price', 'capital stock', 'capitalstock/price'])
for i, p in enumerate(average_price, start=0):
    # print("i {}: price {} cs: {} os: {}, cashshares {}".format(i, p, gcs[i], gos[i], (gcs[i]/p)))
    daily_shares_df =daily_shares_df.append({'price': p, 'capital stock':gcs[i], 'capitalstock/price': (gcs[i]/p)}, ignore_index=True)
daily_shares_df.append({'Date':gcsd}, ignore_index=True)

# daily_shares_df = daily_shares_df.round(decimals=2)
# daily_shares_df = daily_shares_df.append({'Date':gcsd}, ignore_index=True, axis=1)
print(daily_shares_df)

Out:出去:

    Date      price  capital stock  capitalstock/price
0    NaN   9.863333        7251.39        7.351865e+02
1    NaN   9.903333       47200.86        4.766159e+03
2    NaN   9.883333      119020.28        1.204252e+04
3    NaN   9.883333    11751250.39        1.188997e+06
4    NaN   9.883333     4790267.25        4.846813e+05
5    NaN   9.913333      -54597.18       -5.507449e+03
6    NaN   9.933333      -46410.80       -4.672228e+03
7    NaN   9.923333       78669.05        7.927684e+03
8    NaN   9.963333      150819.02        1.513741e+04
9    NaN   9.953333      -23295.45       -2.340467e+03
10   NaN   9.970000       87836.67        8.810097e+03
11   NaN  10.003333        6346.19        6.344075e+02
12   NaN  10.023334       10304.31        1.028032e+03
13   NaN  10.023334     -335114.92       -3.343348e+04
14   NaN  10.023334       94276.75        9.405728e+03
15   NaN  10.020000      -38526.78       -3.844988e+03
16   NaN   9.973333        9998.97        1.002571e+03
17   NaN   9.880000      357659.16        3.620032e+04
18   NaN   9.940000        5487.23        5.520352e+02
19   NaN   9.940000      143213.17        1.440776e+04
20   NaN   9.943334      -25900.72       -2.604833e+03
(pystuff) anthonyloupos@anthonys-MBP pystuff % 

If you append a dataframe without a column, it'll create a null value and you can't overwrite null value through append .如果您附加一个没有列的数据框,它将创建一个null value并且您不能通过append覆盖null value append is used to add rows to the dataframe, not to fillup the null space. append用于向数据帧添加行,而不是填充空空间。

Instead of doing this:而不是这样做:

daily_shares_df = pd.DataFrame(columns = ['Date', 'price', 'capital stock', 'capitalstock/price'])
for i, p in enumerate(average_price, start=0):
    # print("i {}: price {} cs: {} os: {}, cashshares {}".format(i, p, gcs[i], gos[i], (gcs[i]/p)))
    daily_shares_df =daily_shares_df.append({'price': p, 'capital stock':gcs[i], 'capitalstock/price': (gcs[i]/p)}, ignore_index=True)
daily_shares_df.append({'Date':gcsd}, ignore_index=True)

Just simply create a dataframe without the date and then add the date column.只需简单地创建一个没有日期的数据框,然后添加日期列。

daily_shares_df = pd.DataFrame(columns = ['price', 'capital stock', 'capitalstock/price'])
for i, p in enumerate(average_price, start=0):
    daily_shares_df =daily_shares_df.append({'price': p, 'capital stock':gcs[i], 'capitalstock/price': (gcs[i]/p)}, ignore_index=True)

daily_shares_df["date"]=gcsd
# make sure that gcsd will be of list type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM