如何将 pandas 数据帧转换为 python 中的二进制文件

how to convert pandas dataframe to binary file in python

import numpy as np
import pandas as pd
def get_values_for_frequency(freq):
    # sampling information
    Fs = 100# sample rate no of samppes per second
    T = 1/Fs # sampling period   %sample per second
    t = 1 # seconds of sampling
    N = Fs*t # total points in signal
    # signal information
    #freq = 100 # in hertz, 
    omega = 2*np.pi*freq # angular frequency for sine waves
    t_vec = np.arange(N)*T # time vector for plotting
    y = np.sin(omega*t_vec)
    return y
df = pd.DataFrame(columns =['1Hz','2Hz', '3Hz', '4Hz', '5Hz', '6Hz', '7Hz'])
df['1Hz']=pd.Series(get_values_for_frequency(1))
df['2Hz']=pd.Series(get_values_for_frequency(2))
df['3Hz']=pd.Series(get_values_for_frequency(3))
df['4Hz']=pd.Series(get_values_for_frequency(4))
df['5Hz']=pd.Series(get_values_for_frequency(5))
df['6Hz']=pd.Series(get_values_for_frequency(6))
df['7Hz']=pd.Series(get_values_for_frequency(7))
#df.to_csv('samplepersecond.csv')
ndary=df.to_records(index=False)

这是生成正弦波的代码。这里我生成了一个有 7 列(从 1 Hz 到 7 Hz)和 100 行的正弦波。然后我创建了一个 pandas Dataframe 来存储所有这些值。现在,要求是将此 Dataframe 转换为数据类型为 int16 的二进制文件。所以数据帧中的每个值都应该转换成16位有符号整数,然后转换成二进制文件

您可以使用 astype 函数将数据框值转换为 int16。

import numpy as np

df = df.astype(np.int16)

然后您可以使用 to_hdf.

将数据帧保存为 HDF5 格式

df.to_hdf('tmp.hdf','df', mode='w')