python - 从 .csv 文件获取数据

标签 python csv numpy pandas

我正在开发一个 python 项目,其中有一个如下所示的 .csv 文件:

freq,ae,cl,ota
825,1,2,3
835,4,5,6
850,10,11,12
880,22,23,24
910,46,47,48
960,94,95,96
1575,190,191,192
1710,382,383,384
1750,766,767,768

我需要在运行时快速从文件中获取一些数据。
举个例子:

我以 880MHz 的频率进行采样,我想对样本进行一些计算,并利用 .csv 文件第 880 行中的数据。

我通过使用频率冒号作为索引来做到这一点,然后仅使用采样频率来获取数据,但棘手的部分是,如果我使用 900MHz 采样,则会出现错误。我希望它获取下方和上方最接近的数据,在本例中为 880 和 910,从这些数据到行,我将使用这些数据对 900MHz 下的数据进行线性化估计。

我的主要问题是如何快速搜索数据,如果不存在完美匹配,如何获取最近的两行?

最佳答案

取之前的行/系列和之后的行

In [11]: before, after = df1.loc[:900].iloc[-1], df1.loc[900:].iloc[0]

In [12]: before
Out[12]:
ae     22
cl     23
ota    24
Name: 880, dtype: int64

In [13]: after
Out[13]:
ae     46
cl     47
ota    48
Name: 910, dtype: int64

中间放一个空行 interpolate (编辑:默认 interpolation 只会取两者的平均值,因此我们需要设置 method='values'):

In [14]: sandwich = pd.DataFrame([before, pd.Series(name=900), after])

In [15]: sandwich
Out[15]:
     ae  cl  ota
880  22  23   24
900 NaN NaN  NaN
910  46  47   48

In [16]: sandwich.apply(apply(lambda col: col.interpolate(method='values'))
Out[16]:
     ae  cl  ota
880  22  23   24
900  38  39   40
910  46  47   48

In [17]: sandwich.apply(apply(lambda col: col.interpolate(method='values')).loc[900]
Out[17]:
ae     38
cl     39
ota    40
Name: 900, dtype: float64

注意:

df1 = pd.read_csv(csv_location).set_index('freq')

你可以将其包装在某种函数中:

def interpolate_for_me(df, n):
    if n in df.index:
        return df.loc[n]
    before, after = df1.loc[:n].iloc[-1], df1.loc[n:].iloc[0]
    sandwich = pd.DataFrame([before, pd.Series(name=n), after])
    return sandwich.apply(lambda col: col.interpolate(method='values')).loc[n]

关于python - 从 .csv 文件获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16614712/

相关文章:

python - 在 numpy 中强制非数字字符为 NA(将 csv 读取到 pandas 数据帧时)

python - 如何使用 NumPy ndarray 从 HDF5 数据集共享内存

python - 如何继承 python 生成器并覆盖 __iter__

python - 如何在本地安装和访问python模块?

python - 阴影去除道路图像

PHP 在 Internet Explorer 中显示 CSV 文件

Python - 通过读取文本文件并搜索该词典来创建词典

python - 如何处理 CSV 的列不一致

python - 从压缩数据列表创建一个非常大的稀疏矩阵 csv

algorithm - 涉及任意两个节点的三角形数