将现有列作为移动平均值复制到数据框

我想我想多了——我正在尝试复制现有的 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())

我得到了我需要的数据:

http://img.mukewang.com/64913e810001f32905210198.jpg

我的问题只是将其保存为一个新的数据框,我可以将其连接到旧的,然后进行分析。


编辑


我现在已经能够制作一堆数据框,但我想将它们连接在一起,这就是问题所在:


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"


繁花如伊
浏览 134回答 1
1回答

FFIVE

您应该遍历列名并将生成的 pandas 系列分配为新的命名列,例如:import pandas as pdoriginal_data = pd.DataFrame({'A': range(100), 'B': range(100, 200)})ma_columns = ['A', 'B']for column in ma_columns:&nbsp; &nbsp; new_column = column + '_ma'&nbsp; &nbsp; original_data[new_column] = pd.DataFrame(original_data[column].rolling(window=7).mean())print(original_data)输出数据帧:&nbsp; &nbsp; A&nbsp; &nbsp; B&nbsp; A_ma&nbsp; &nbsp;B_ma0&nbsp; &nbsp; 0&nbsp; 100&nbsp; &nbsp;NaN&nbsp; &nbsp; NaN1&nbsp; &nbsp; 1&nbsp; 101&nbsp; &nbsp;NaN&nbsp; &nbsp; NaN2&nbsp; &nbsp; 2&nbsp; 102&nbsp; &nbsp;NaN&nbsp; &nbsp; NaN3&nbsp; &nbsp; 3&nbsp; 103&nbsp; &nbsp;NaN&nbsp; &nbsp; NaN4&nbsp; &nbsp; 4&nbsp; 104&nbsp; &nbsp;NaN&nbsp; &nbsp; NaN..&nbsp; ..&nbsp; ...&nbsp; &nbsp;...&nbsp; &nbsp; ...95&nbsp; 95&nbsp; 195&nbsp; 92.0&nbsp; 192.096&nbsp; 96&nbsp; 196&nbsp; 93.0&nbsp; 193.097&nbsp; 97&nbsp; 197&nbsp; 94.0&nbsp; 194.098&nbsp; 98&nbsp; 198&nbsp; 95.0&nbsp; 195.099&nbsp; 99&nbsp; 199&nbsp; 96.0&nbsp; 196.0[100 rows x 4 columns]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python