df = ts_get_df(ts_code='002452.SZ', start_date='20210101', end_date='20211129')
print(df.tail(2))
hd = intelligent_assistant(df)
print(hd.tail(2))
target = pd.concat([df, hd], axis=1, ignore_index=False)
print(target.tail(2))
date open high low close vol
217 2021-11-26 9.78 10.49 9.56 9.82 734842.83
218 2021-11-29 9.82 10.78 9.66 10.54 511907.42
hdb hdc
217 8.99 8.79
218 9.14 8.89
date open high low close vol hda hdb hdc
217 2021-11-26 9.78 10.49 9.56 9.82 734842.83 9.881667 8.99 8.79
218 2021-11-29 9.82 10.78 9.66 10.54 511907.42 10.313333 9.14 8.89
打印df hd的columns看看
ignore_index=False
改为
ignore_index=True
有帮助,请点击右上角采纳
在 intelligent_assistant()里返回的有这一列,所以合并时出现该列,如果只有两列数据的话,合并结果正常。见如下:
import tushare as ts
import pandas as pd
df = ts.get_k_data(code='002452', start='2021-01-01', end='2021-11-29')
# hd = ts.intelligent_assistant(df)
# print(hd.tail(2))
hd=pd.DataFrame({'hdb':[8.99,9.14],'hdc':[8.79,8.89]},index=[217,218])
target = pd.concat([df.tail(2), hd], axis=1, ignore_index=False)
print(target.tail(2))
date open close high low volume code hdb hdc
217 2021-11-26 9.78 9.82 10.49 9.56 734843.0 002452 8.99 8.79
218 2021-11-29 9.82 10.54 10.78 9.66 511907.0 002452 9.14 8.89
问题找到了,是df作为入参传进intelligent_assistant()后被改变了原来的结构,在concat时用的是新的df而不是原来的df了,而被改变的df增加了‘hda’列,故而有此结果。
解决方法有二:
一:
对df 进行copy后再传入intelligent_assistant()里;
二:
在intelligent_assistant()里把新赋值的值给drop掉。