我有一个看起来像这样的 pandas DataFrame:
I've got a pandas DataFrame that looks like this:
sum
1948 NaN
1949 NaN
1950 5
1951 3
1952 NaN
1953 4
1954 8
1955 NaN
我想只在开头和结尾截断 NaN(即只保留从 1950 到 1954 的值,包括 NaN).我已经尝试过 .isnull() 和 dropna(),但不知何故我找不到合适的解决方案.有人可以帮忙吗?
and I would like to cut off the NaNs at the beginning and at the end ONLY (i.e. only the values incl. NaN from 1950 to 1954 should remain).
I already tried .isnull() and dropna(), but somehow I couldn't find a proper solution.
Can anyone help?
使用内置的first_valid_index 和 last_valid_index 它们是专门为此设计的,并对你的 df 进行切片:
Use the built in first_valid_index and last_valid_index they are designed specifically for this and slice your df:
In [5]:
first_idx = df.first_valid_index()
last_idx = df.last_valid_index()
print(first_idx, last_idx)
df.loc[first_idx:last_idx]
1950 1954
Out[5]:
sum
1950 5
1951 3
1952 NaN
1953 4
1954 8
这篇关于Pandas:仅在数据帧的开头和结尾删除 NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 conda 环境中安装 Selenium?How to install Selenium in a conda environment?(如何在 conda 环境中安装 Selenium?)
使用 Anaconda installe 在 Windows 上获取 CUDA 和 CUDNNget the CUDA and CUDNN version on windows with Anaconda installe(使用 Anaconda installe 在 Windows 上获取 CUDA 和 CUDNN 版本)
如何下载适用于 python 3.6 的 AnacondaHow can I download Anaconda for python 3.6(如何下载适用于 python 3.6 的 Anaconda)
使用两个不同的 Python 发行版Using two different Python Distributions(使用两个不同的 Python 发行版)
除了 OSX 上现有的 pyenv 安装之外,如何安装 AnaHow can I install Anaconda aside an existing pyenv installation on OSX?(除了 OSX 上现有的 pyenv 安装之外,如何安装 Anaconda?)
在 Cygwin 中为 Anaconda 永久设置 Python 路径Permanently set Python path for Anaconda within Cygwin(在 Cygwin 中为 Anaconda 永久设置 Python 路径)