我正在使用时间戳索引pandas.DataFrame.resample重新采样分组的 Pandas dataframe。
在其中一列中,我想重新采样,以便选择最常见的值。目前,我只成功使用 NumPy 函数,如np.max或np.sum等。
#generate test dataframe
data = np.random.randint(0,10,(366,2))
index = pd.date_range(start=pd.Timestamp('1-Dec-2012'), periods=366, unit='D')
test = pd.DataFrame(data, index=index)
#generate group array
group = np.random.randint(0,2,(366,))
#define how dictionary for resample
how_dict = {0: np.max, 1: np.min}
#perform grouping and resample
test.groupby(group).resample('48 h',how=how_dict)
Run Code Online (Sandbox Code Playgroud)
前面的代码有效,因为我使用了 NumPy 函数。但是,如果我想按最频繁的值使用重采样,我不确定。我尝试定义一个自定义函数,如
def frequent(x):
(value, counts) = np.unique(x, return_counts=True)
return value[counts.argmax()]
Run Code Online (Sandbox Code Playgroud)
但是,如果我现在这样做:
how_dict = {0: np.max, 1: frequent}
Run Code Online (Sandbox Code Playgroud)
我得到一个空的数据框...
df = test.groupby(group).resample('48 h',how=how_dict)
df.shape
Run Code Online (Sandbox Code Playgroud) 如果我使用像http://www.h-schmidt.net/FloatConverter/IEEE754.html这样的网站将十六进制字符串'424E4B31'转换为 float32,我得到 51.57343。
我需要使用 Python 来转换字符串,但是,使用 StackExchange 上的解决方案,例如:
import struct, binascii
hexbytes = b"\x42\x4E\x4B\x31"
struct.unpack('<f',hexbytes)
Run Code Online (Sandbox Code Playgroud)
或者
struct.unpack('f', binascii.unhexlify('424E4B31'))
Run Code Online (Sandbox Code Playgroud)
我得到 2.9584e-09... 为什么不同?