gpt4 book ai didi

python - 将 DataFrame 与其自身合并/append 会产生新列

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:59 29 4
gpt4 key购买 nike

我有以下合并操作:

data_static = pandas.merge(data_static, data_output[['TICKER', 'DATE', 'rolling_vola_40', 'rolling_vola_80', 'f_rolling_vola_40', 'f_rolling_vola_80', 'rolling_vola_prev_annum', 'rolling_vola_post_annum']], how='left', on=['TICKER', 'DATE'])

我现在的问题是这会导致以下标题:

;YEAR;DATE;TICKER;LONG_COMP_NAME;ISSUER_INDUSTRY;INDUSTRY_SECTOR;COUNTRY;ACCOUNTING_STANDARD;ACCOUNTING_STANDARD_OVERRIDE;EQY_FUND_CRNCY;INDEX;DATE_PREV;DATE_NEXT;rolling_vola_40_x;rolling_vola_80_x;f_rolling_vola_40_x;f_rolling_vola_80_x;rolling_vola_prev_annum_x;rolling_vola_post_annum_x;rolling_vola_40_y;rolling_vola_80_y;f_rolling_vola_40_y;f_rolling_vola_80_y;rolling_vola_prev_annum_y;rolling_vola_post_annum_y

我希望数据进入同一列,如下所示:

;YEAR;DATE;TICKER;LONG_COMP_NAME;ISSUER_INDUSTRY;INDUSTRY_SECTOR;COUNTRY;ACCOUNTING_STANDARD;ACCOUNTING_STANDARD_OVERRIDE;EQY_FUND_CRNCY;INDEX;DATE_PREV;DATE_NEXT;rolling_vola_40;rolling_vola_80;f_rolling_vola_40;f_rolling_vola_80;rolling_vola_prev_annum;rolling_vola_post_annum;

因此,不要像这样彼此相邻(示例):

    TICKER   Val1_x   Val2_x   Val3_x   Val1_y   Val2_y   Val3_y
A 80 6 1 NaN NaN NaN
B NaN NaN NaN 10 12 14

我希望它们是这样的:

    TICKER   Val1     Val2     Val3
A 80 6 1
B 10 12 14

我的合并联接在 TICKERDATE 列上,因此不要被示例数据混淆。

最佳答案

这里的解决方法是首先使用append,然后在最后merge它们:

data_store = pandas.DataFrame(columns=('TICKER', 'DATE', 'rolling_vola_40', 'rolling_vola_80', 'f_rolling_vola_40', 'f_rolling_vola_80', 'rolling_vola_prev_annum', 'rolling_vola_post_annum'))

for index, row in data_static.iterrows():
data_output = vol(row['TICKER'], row['DATE'], row['DATE_PREV'], row['DATE_NEXT'])
if type(data_output) != type(None):
data_store = data_store.append(data_output[['TICKER', 'DATE', 'rolling_vola_40', 'rolling_vola_80', 'f_rolling_vola_40', 'f_rolling_vola_80', 'rolling_vola_prev_annum', 'rolling_vola_post_annum']])

data_static = pandas.merge(data_static, data_store[['TICKER', 'DATE', 'rolling_vola_40', 'rolling_vola_80', 'f_rolling_vola_40', 'f_rolling_vola_80', 'rolling_vola_prev_annum', 'rolling_vola_post_annum']], how='left', on=['TICKER', 'DATE'])
data_static.to_csv('test.csv', sep=';', encoding='utf-8')

关于python - 将 DataFrame 与其自身合并/append 会产生新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38872881/

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