熊猫康卡特新专栏

时间:2017-05-02 05:22:55

标签: python-3.x pandas concat

为什么我在“行动”中获得NaN'柱? 对我来说,我得到的结果似乎很奇怪。我尝试过使用ignore_index = True并且它有一个freq错误。

                        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)

2 个答案:

答案 0 :(得分:1)

您需要相同的索引 - SeriesDataFrame进行对齐,否则获取NaN s:

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

如果valuesnumpy 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

答案 1 :(得分:0)

如果我理解正确,您只想在数据框中添加一列。如果是这样,这是最简单的方法。

df['Action'] = buy_stp