【问题标题】:How come pd.concat(axis=1) is row-binding instead of column binding?为什么 pd.concat(axis=1) 是行绑定而不是列绑定?
【发布时间】:2019-12-19 13:23:56
【问题描述】:

我正在使用pandas,并且我有两个具有相同行数的数据框:

我想做的就是通过ticker将它们合并在一起,我试过了

a_no_nan2_last.join(ref, on = "ticker")

这给了我这个错误

ValueError:您正在尝试合并 object 和 int64 列。如果 你想继续你应该使用 pd.concat

所以我检查了两者中 ticker 列的类型,并检查了 ticker 是唯一重叠的列

set(a_no_nan2_last.columns).intersection(set(res.columns))

这给了

{'ticker'}

type(a_no_nan2_last["ticker"].iloc[0])
<class 'str'>
type(res["ticker"].iloc[0])
<class 'str'>

数据看起来像

a_no_nan2_last
       ticker       Date   Close  Close_lead1
2141      1AD 2018-08-14  0.2845        0.160
5354      1AG 2018-08-14  0.0450        0.036
75158     1AL 2018-08-14  0.9287        0.800
188       1ST 2018-08-14  0.0370        0.079
81195     3DP 2018-08-14  0.0320        0.041
       ...        ...     ...          ...
111688    ZMI 2018-08-14  0.1200        0.095
111310    ZNC 2018-08-14  0.1650        0.078
111877    ZNO 2018-08-13  0.1150        0.079
111373    ZTA 2018-08-10  0.0700        0.070
111940    ZYB 2018-08-09  0.0070        0.014
[1792 rows x 4 columns]

res
variable ticker  ...  Close__variance_larger_than_standard_deviation
0           1AD  ...                                             0.0
1           1AG  ...                                             0.0
2           1AL  ...                                             0.0
3           1ST  ...                                             0.0
4           3DP  ...                                             0.0
         ...  ...                                             ...
1787        ZMI  ...                                             0.0
1788        ZNC  ...                                             0.0
1789        ZNO  ...                                             0.0
1790        ZTA  ...                                             0.0
1791        ZYB  ...                                             0.0
[1792 rows x 795 columns]

所以我尝试了 concat,它是按行而不是按列追加的,这给了我错误的结果

pd.concat([a_no_nan2_last, res], axis=1)
       ticker  ... Close__variance_larger_than_standard_deviation
0         NaN  ...                                            0.0
1         NaN  ...                                            0.0
2         NaN  ...                                            0.0
3         NaN  ...                                            0.0
4         NaN  ...                                            0.0
       ...  ...                                            ...
111688    ZMI  ...                                            NaN
111751    Z1P  ...                                            NaN
111814    ZIP  ...                                            NaN
111877    ZNO  ...                                            NaN
111940    ZYB  ...                                            NaN
[3556 rows x 799 columns]

我预计我的输出长度为 1792 行。

删除公共列 ticker 也无济于事,请参阅

res3 = res.drop("ticker", axis=1)
pd.concat([a_no_nan2_last, res3], axis=1)

       ticker  ... Close__variance_larger_than_standard_deviation
0         NaN  ...                                            0.0
1         NaN  ...                                            0.0
2         NaN  ...                                            0.0
3         NaN  ...                                            0.0
4         NaN  ...                                            0.0
       ...  ...                                            ...
111688    ZMI  ...                                            NaN
111751    Z1P  ...                                            NaN
111814    ZIP  ...                                            NaN
111877    ZNO  ...                                            NaN
111940    ZYB  ...                                            NaN
[3556 rows x 798 columns]

【问题讨论】:

标签: python pandas


【解决方案1】:

您需要reset_index(drop=True) 例如

res1 = pd.concat([a_no_nan2_last.reset_index(drop=True), res.reset_index(drop=True)], axis=1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    相关资源
    最近更新 更多