python - 尝试在 pandas 数据框中跨行插值时出错

标签 python pandas dataframe

我想按行插入附加 csv 中的数据帧(即按行填充 NaN)。 csv 在这里:https://www.dropbox.com/s/v7ee5d25tvxo625/pd_fill.csv?dl=0

但是,将 csv 读入 pandas 数据帧后,如下所示。代码我收到错误:

df.interpolate(axis=1)

*** TypeError: Cannot interpolate with all NaNs.

如何解决此问题并跨行进行插值?

最佳答案

您只能插入相应的(数字)列。

我们将使用.filter(regex=<RE>)函数以过滤列:

In [33]: df.filter(regex='^19|20').columns
Out[33]:
Index(['1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982',
       '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991',
       '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000',
       '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009',
       '2010', '2011', '2012', '2013', '2014', '2015'],
      dtype='object')

所以你可以使用:

df.filter(regex='^19|20').interpolate(axis=1)

示例(仅前 5 行):

In [34]: df.filter(regex='^19|20').interpolate(axis=1).head()
Out[34]:
       1974           1975           1976           1977           1978  \
0       0.0     250.000000     500.000000     750.000000    1000.000000
1  100000.0  130000.000000  160000.000000  190000.000000  220000.000000
2       0.0       6.871795      13.743590      20.615385      27.487179
3       0.0    9230.769231   18461.538462   27692.307692   36923.076923
4       0.0   19000.000000   38000.000000   57000.000000   92100.000000

            1979           1980           1981           1982           1983  \
0    1250.000000    1500.000000    1750.000000    2000.000000    2800.000000
1  250000.000000  280000.000000  310000.000000  340000.000000  370000.000000
2      34.358974      41.230769      48.102564      54.974359      61.846154
3   46153.846154   55384.615385   64615.384615   73846.153846   83076.923077
4  127200.000000  162300.000000  197400.000000  232500.000000  316000.000000

      ...              2006          2007          2008          2009  \
0     ...      2.516660e+07  2.770800e+07  2.753100e+07  2.735400e+07
1     ...      1.166667e+07  1.433333e+07  1.700000e+07  1.416962e+07
2     ...      2.198974e+02  2.267692e+02  2.336410e+02  2.405128e+02
3     ...      6.394286e+05  7.060000e+05  7.060000e+05  7.060000e+05
4     ...      2.550200e+07  2.655350e+07  2.760500e+07  2.865650e+07

           2010          2011          2012          2013        2014  \
0  2.717700e+07  2.700000e+07  2.809050e+07  2.918100e+07  29181000.0
1  1.133923e+07  8.508848e+06  5.678463e+06  2.848079e+06     17695.0
2  2.473846e+02  2.542564e+02  2.611282e+02  2.680000e+02       268.0
3  7.060000e+05  7.060000e+05  7.060000e+05  7.060000e+05    706000.0
4  2.970800e+07  3.075950e+07  3.181100e+07  3.181100e+07  31811000.0

         2015
0  29181000.0
1     17695.0
2       268.0
3    706000.0
4  31811000.0

[5 rows x 42 columns]

关于python - 尝试在 pandas 数据框中跨行插值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36847821/

相关文章:

python - 在 Python 中将一个数据帧中的值替换为第二个数据帧中的值

python - 如何根据另一列的值在 Pandas 数据框列中创建新值

Python:计算 Pandas 系列中值的累积出现次数

python - gevent-socketio 从线程发送消息

Python 套接字和 Opencv - ConnectionResetError : [WinError 10054]

python - 带有 pandas 的日期正则表达式过滤器不起作用

python-3.x - 如何使用 pandas 中的条件从两个 SQLite 表创建 Excel 文件?

r - 随着时间的推移逐行比较 2 个数据帧的分析

python - 如何处理 Pandas 中的 SettingWithCopyWarning

python - 在 Python 中通过 strptime 解析不带前导零的时间