亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將現有列作為移動平均值復制到數據框

將現有列作為移動平均值復制到數據框

繁花如伊 2023-06-20 13:51:38
我想我想多了——我正在嘗試復制現有的 pandas 數據框列和值并進行滾動平均——我不想覆蓋原始數據。我正在遍歷列,獲取列和值,將滾動的 7 天 ma 作為新列,后綴_ma作為原始副本的副本。我想將現有數據與 7 天 MA 進行比較,并查看數據來自 7 天 MA 的標準偏差 - 我可以弄清楚 - 我只是想將 MA 數據保存為新數據框。我有for column in original_data[ma_columns]:    ma_df = pd.DataFrame(original_data[ma_columns].rolling(window=7).mean(), columns = str(column)+'_ma')并得到錯誤:Index(...) must be called with a collection of some kind, 'Carrier_AcctPswd_ma' was passed但是如果我迭代for column in original_data[ma_columns]:    print('Colunm Name : ', str(column)+'_ma')    print('Contents : ', original_data[ma_columns].rolling(window=7).mean())我得到了我需要的數據:我的問題只是將其保存為一個新的數據框,我可以將其連接到舊的,然后進行分析。編輯我現在已經能夠制作一堆數據框,但我想將它們連接在一起,這就是問題所在:for column in original_data[ma_columns]:    MA_data = pd.DataFrame(original_data[column].rolling(window=7).mean())    for i in MA_data:        new = pd.concat(i)        print(i)<ipython-input-75-7c5e5fa775b3> in <module>     17 #     print(type(MA_data))     18     for i in MA_data:---> 19         new = pd.concat(i)     20         print(i)     21 ~\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy)    279         verify_integrity=verify_integrity,    280         copy=copy,--> 281         sort=sort,    282     )    283 ~\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in __init__(self, objs, axis, join, keys, levels, names, ignore_index, verify_integrity, copy, sort)    307                 "first argument must be an iterable of pandas "    308                 "objects, you passed an object of type "--> 309                 '"{name}"'.format(name=type(objs).__name__)    310             )    311 TypeError: first argument must be an iterable of pandas objects, you passed an object of type "str"
查看完整描述

1 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

您應該遍歷列名并將生成的 pandas 系列分配為新的命名列,例如:


import pandas as pd


original_data = pd.DataFrame({'A': range(100), 'B': range(100, 200)})


ma_columns = ['A', 'B']


for column in ma_columns:

    new_column = column + '_ma'

    original_data[new_column] = pd.DataFrame(original_data[column].rolling(window=7).mean())


print(original_data)

輸出數據幀:


    A    B  A_ma   B_ma

0    0  100   NaN    NaN

1    1  101   NaN    NaN

2    2  102   NaN    NaN

3    3  103   NaN    NaN

4    4  104   NaN    NaN

..  ..  ...   ...    ...

95  95  195  92.0  192.0

96  96  196  93.0  193.0

97  97  197  94.0  194.0

98  98  198  95.0  195.0

99  99  199  96.0  196.0


[100 rows x 4 columns]


查看完整回答
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號