【问题标题】:Copying existing columns as moving averages to a dataframe将现有列作为移动平均值复制到数据框
【发布时间】:2020-12-12 15:47:23
【问题描述】:

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

【问题讨论】:

  • 你试过使用熊猫内置的滚动功能吗?这是一个例子datacamp.com/community/tutorials/moving-averages-in-pandas
  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。
  • columns= 可能需要列列表 - 即使您只有一列。
  • 再次:显示完整的错误消息(从单词 Traceback 开始)。还有其他有用的信息 - 即。哪一行有问题。

标签: python pandas loops analysis


【解决方案1】:

您应该遍历列名并将生成的 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]

【讨论】:

  • 君子文人,谢谢你的帮助
猜你喜欢
  • 2020-12-26
  • 2021-04-28
  • 1970-01-01
  • 2021-09-07
  • 2020-11-26
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多