我有一张 table :
-60 -40 -20 0 20 40 60
100 520 440 380 320 280 240 210
110 600 500 430 370 320 280 250
120 670 570 490 420 370 330 290
130 740 630 550 480 420 370 330
140 810 690 600 530 470 410 370
顶部的标题是风矢量,左侧的第一列是距离。表格“主体”中的实际数据只是燃油添加剂。
我对 Pandas 和 Numpy 很陌生,所以请原谅这个问题的简单性。我想知道的是,如何使用标题进入表格来检索一个数字?我已经看到它可以使用索引,但如果不需要,我不想使用该方法。
例如:
我的风单位为 -60
,距离为 120
,因此我需要检索数字 670
。如何使用 Numpy 或 Pandas 来执行此操作?
此外,如果我的风单位为 -50
且距离为 125
,是否可以通过简单的方式对它们进行插值?
编辑:
这是我迄今为止尝试过的:
import pandas as pd
df = pd.read_table('fuel_adjustment.txt', delim_whitespace=True, header=0,index_col=0)
print(df.loc[120, -60])
但出现错误:
line 3083, in get_loc raise KeyError(key) from err
KeyError: -60
最佳答案
您可以使用以下方法从现有索引中选择任何单元格:
df.loc[120,-60]
但是索引的类型必须是整数。如果没有,您可以使用以下方法修复它:
df.index = df.index.map(int)
df.columns = df.columns.map(int)
对于插值,您需要使用重新索引
添加空的新行/列,然后应用 interpolate
在每个维度上。
(df.reindex(index=sorted(df.index.to_list()+[125]),
columns=sorted(df. columns.to_list()+[-50]))
.interpolate(axis=1, method='index')
.interpolate(method='index')
)
输出:
-60 -50 -40 -20 0 20 40 60
100 520.0 480.0 440.0 380.0 320.0 280.0 240.0 210.0
110 600.0 550.0 500.0 430.0 370.0 320.0 280.0 250.0
120 670.0 620.0 570.0 490.0 420.0 370.0 330.0 290.0
125 705.0 652.5 600.0 520.0 450.0 395.0 350.0 310.0
130 740.0 685.0 630.0 550.0 480.0 420.0 370.0 330.0
140 810.0 750.0 690.0 600.0 530.0 470.0 410.0 370.0
关于python - 如何使用 Pandas 获取表格中的单个单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70502488/