Python Pandas 日均值 [英] Python Pandas daily average

查看:76
本文介绍了Python Pandas 日均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Pandas 数据库中获取每日平均值时遇到问题.我在这里检查了 使用熊猫从不规则时间序列计算每日平均值 并没有帮助.csv 文件如下所示:

I'm having problems getting the daily average in a Pandas database. I've checked here Calculating daily average from irregular time series using pandas and it doesn't help. csv files look like this:

Date/Time,Value
12/08/13 12:00:01,5.553
12/08/13 12:30:01,2.604
12/08/13 13:00:01,2.604
12/08/13 13:30:01,2.604
12/08/13 14:00:01,2.101
12/08/13 14:30:01,2.666

等等.我的代码如下所示:

and so on. My code looks like this:

# Import iButton temperatures
flistloc = '../data/iButtons/Readings/edit'
flist = os.listdir(flistloc)
# Create empty dictionary to store db for each file
pdib = {}
for file in flist:
    file = os.path.join(flistloc,file)
    # Calls function to return only name
    fname,_,_,_= namer(file)
    # Read each file to db
    pdib[fname] = pd.read_csv(file, parse_dates=0, dayfirst=True, index_col=0)
pdibkeys = sorted(pdib.keys())
#
# Calculate daily average for each iButton
for name in pdibkeys:
    pdib[name]['daily'] = pdib[name].resample('D', how = 'mean')```

数据库看起来没问题,但平均不起作用.这是在 iPython 中的样子:

The database seems ok but the averaging doesn't work. Here is what one looks like in iPython:

'2B5DE4': <class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1601 entries, 2013-08-12 12:00:01 to 2013-09-14 20:00:01
Data columns (total 2 columns):
Value    1601  non-null values
daily    0  non-null values
dtypes: float64(2)}

有人知道这是怎么回事吗?

Anyone know what's going on?

推荐答案

这个问题有点老了,但我还是想贡献一下,因为我不得不一遍又一遍地处理这个问题(我认为它不是真正的 Pythonic...).

The question is somewhat old, but i want to contribute anyway since i had to deal with this over and over again (and i think it's not really pythonic...).

到目前为止,我提出的最佳解决方案是使用原始索引创建一个主要为 NA 的新数据框,并在最后填充它.

The best solution, i have come up so far is to use the original index to create a new dataframe with mostly NA and fill it up at the end.

davg = df.resample('D', how='mean')
davg_NA = davg.loc[df.index]
davg_daily = davg_NA.fillna(method='ffill')

一个人甚至可以把它挤在一行中

One can even cramp this in one line

df.resample('D', how='mean').loc[df.index].fillna(method='ffill')

这篇关于Python Pandas 日均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆