gpt4 book ai didi

python-3.x - Pandas Concat 新专栏

转载 作者:行者123 更新时间:2023-12-05 06:40:03 25 4
gpt4 key购买 nike

为什么我在“ACTION”列中得到 NaN?我得到那个结果对我来说似乎很奇怪。我试过使用 ignore_index = True 并且它有一个频率错误。

                        C     H     L     O     OI       V   WAP  ACTION
datetime
2017-03-14 00:52:00 8.25 8.25 8.19 8.21 302.0 1769.0 8.22 NaN
2017-03-13 23:54:00 8.09 8.10 8.09 8.10 6.0 65.0 8.10 NaN
2017-03-14 01:03:00 8.29 8.32 8.28 8.29 175.0 1084.0 8.30 NaN
2017-03-14 00:03:00 8.15 8.15 8.14 8.15 13.0 50.0 8.15 NaN
2017-03-13 23:57:00 8.13 8.13 8.12 8.12 3.0 6.0 8.12 NaN

我想得到-

                        C     H     L     O     OI       V   WAP  ACTION
datetime
2017-03-14 00:52:00 8.25 8.25 8.19 8.21 302.0 1769.0 8.22 100
2017-03-13 23:54:00 8.09 8.10 8.09 8.10 6.0 65.0 8.10 200
2017-03-14 01:03:00 8.29 8.32 8.28 8.29 175.0 1084.0 8.30 300
2017-03-14 00:03:00 8.15 8.15 8.14 8.15 13.0 50.0 8.15 400
2017-03-13 23:57:00 8.13 8.13 8.12 8.12 3.0 6.0 8.12 500

buy_stp = pd.Series([100,200,300,400,500],name= 'ACTION')
print(buy_stp)
df10 = pd.concat([df_concat_results,
buy_stp],
axis=1,
join_axes=[df_concat_results.index])

print(df10)

最佳答案

您需要相同的索引 - SeriesDataFrame 用于对齐,否则得到 NaNs:

buy_stp.index = df.index
df['ACTION'] = buy_stp
print (df)
C H L O OI V WAP ACTION
datetime
2017-03-14 00:52:00 8.25 8.25 8.19 8.21 302.0 1769.0 8.22 100
2017-03-13 23:54:00 8.09 8.10 8.09 8.10 6.0 65.0 8.10 200
2017-03-14 01:03:00 8.29 8.32 8.28 8.29 175.0 1084.0 8.30 300
2017-03-14 00:03:00 8.15 8.15 8.14 8.15 13.0 50.0 8.15 400
2017-03-13 23:57:00 8.13 8.13 8.12 8.12 3.0 6.0 8.12 500

或者:

buy_stp = pd.Series([100,200,300,400,500],name= 'ACTION', index=df.index)
print(buy_stp)
datetime
2017-03-14 00:52:00 100
2017-03-13 23:54:00 200
2017-03-14 01:03:00 300
2017-03-14 00:03:00 400
2017-03-13 23:57:00 500
Name: ACTION, dtype: int64

df['ACTION'] = buy_stp
print (df)
C H L O OI V WAP ACTION
datetime
2017-03-14 00:52:00 8.25 8.25 8.19 8.21 302.0 1769.0 8.22 100
2017-03-13 23:54:00 8.09 8.10 8.09 8.10 6.0 65.0 8.10 200
2017-03-14 01:03:00 8.29 8.32 8.28 8.29 175.0 1084.0 8.30 300
2017-03-14 00:03:00 8.15 8.15 8.14 8.15 13.0 50.0 8.15 400
2017-03-13 23:57:00 8.13 8.13 8.12 8.12 3.0 6.0 8.12 500

如果通过 values 转换为 numpy array 也有效或者list,只需要相同长度的dfbuy_stp:

df['ACTION'] = buy_stp.values
print (df)
C H L O OI V WAP ACTION
datetime
2017-03-14 00:52:00 8.25 8.25 8.19 8.21 302.0 1769.0 8.22 100
2017-03-13 23:54:00 8.09 8.10 8.09 8.10 6.0 65.0 8.10 200
2017-03-14 01:03:00 8.29 8.32 8.28 8.29 175.0 1084.0 8.30 300
2017-03-14 00:03:00 8.15 8.15 8.14 8.15 13.0 50.0 8.15 400
2017-03-13 23:57:00 8.13 8.13 8.12 8.12 3.0 6.0 8.12 500

df['ACTION'] = buy_stp.tolist()
print (df)
C H L O OI V WAP ACTION
datetime
2017-03-14 00:52:00 8.25 8.25 8.19 8.21 302.0 1769.0 8.22 100
2017-03-13 23:54:00 8.09 8.10 8.09 8.10 6.0 65.0 8.10 200
2017-03-14 01:03:00 8.29 8.32 8.28 8.29 175.0 1084.0 8.30 300
2017-03-14 00:03:00 8.15 8.15 8.14 8.15 13.0 50.0 8.15 400
2017-03-13 23:57:00 8.13 8.13 8.12 8.12 3.0 6.0 8.12 500

关于python-3.x - Pandas Concat 新专栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43730628/

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