在Pandas DataFrame中查找第一个和最后一个非NaN值

90

我有一个以日期为索引的Pandas DataFrame。有许多列,但是很多列只在时间序列的一部分填充了值。我想找到第一个和最后一个非NaN值的位置,以便我可以提取日期并查看特定列的时间序列有多长。

请问有人能指点我如何实现这样的操作吗?


50
first_valid_indexlast_valid_index 是Pandas 库中的两个函数,用于获取 Series 对象中第一个和最后一个非空值的索引位置。使用这两个函数可以方便地找到数据集中的有效数据范围。 - behzad.nouri
当缺失值可以是“0”时,是否有解决方案?(即查找每个组/时间序列的第一个非零值)? - GrimSqueaker
3个回答

73

1
@KorayTugay pandas Series 是一维的(即单列)。如果您想在 df 中检查更多列,可以迭代您的 df。例如:for col_name, data in df.items(): print("First valid index for column {} is at {}".format(col_name, data.first_valid_index())) - Jason
3
你可以使用 df.apply(Series.first_valid_index) 来代替对 DataFrame 列的迭代。 - Peque

55

这里有一些有用的例子。

系列

s = pd.Series([np.NaN, 1, np.NaN, 3, np.NaN], index=list('abcde'))
s

a    NaN
b    1.0
c    NaN
d    3.0
e    NaN
dtype: float64

# first valid index
s.first_valid_index()
# 'b'

# first valid position
s.index.get_loc(s.first_valid_index())
# 1

# last valid index
s.last_valid_index()
# 'd'

# last valid position
s.index.get_loc(s.last_valid_index())
# 3

使用notnaidxmax的替代方案:

# first valid index
s.notna().idxmax()
# 'b'

# last valid index
s.notna()[::-1].idxmax()
# 'd'

DataFrame

df = pd.DataFrame({
    'A': [np.NaN, 1, np.NaN, 3, np.NaN], 
    'B': [1, np.NaN, np.NaN, np.NaN, np.NaN]
})
df

     A    B
0  NaN  1.0
1  1.0  NaN
2  NaN  NaN
3  3.0  NaN
4  NaN  NaN

(first|last)_valid_index在数据框(DataFrames)中未定义,但您可以使用apply函数在每一列上应用它们。

# first valid index for each column
df.apply(pd.Series.first_valid_index)

A    1
B    0
dtype: int64

# last valid index for each column
df.apply(pd.Series.last_valid_index)

A    3
B    0
dtype: int64

和之前一样,您也可以使用notnaidxmax。这是稍微更自然的语法。

# first valid index
df.notna().idxmax()

A    1
B    0
dtype: int64

# last valid index
df.notna()[::-1].idxmax()

A    3
B    0
dtype: int64

3
idxmax() 的问题在于,对于一个完全由 NaN 组成的列,它会返回 0。在这种情况下,我希望返回 NaN,因此我更愿意始终使用 .apply(Series.first_valid_index) - Peque
1
对于整个数据框,您可以使用 df.apply(pd.Series.first_valid_index).max() 查找第一个没有 NaN 的索引。 - pseudoabdul

1

这是一个基于behzad.nouri的代码和cs95之前的回答的方便函数。任何错误或误解都是我的责任。

import pandas as pd
import numpy as np

df = pd.DataFrame([["2022-01-01", np.nan, np.nan, 1], ["2022-01-02", 2, np.nan, 2], ["2022-01-03", 3, 3, 3], ["2022-01-04", 4, 4, 4], ["2022-01-05", np.nan, 5, 5]], columns=['date', 'A', 'B', 'C'])
df['date'] = pd.to_datetime(df['date'])

df
#        date    A    B    C
#0 2022-01-01  NaN  NaN  1.0
#1 2022-01-02  2.0  NaN  2.0
#2 2022-01-03  3.0  3.0  3.0
#3 2022-01-04  4.0  4.0  4.0
#4 2022-01-05  NaN  5.0  5.0

我们希望从A和B共同拥有的最早日期开始,并在A和B共同拥有的最晚日期结束(无论何种原因,我们不按C列筛选)。
# filter data to minimum/maximum common available dates
def get_date_range(df, cols):
    """return a tuple of the earliest and latest valid data for all columns in the list"""
    a,b = df[cols].apply(pd.Series.first_valid_index).max(), df[cols].apply(pd.Series.last_valid_index).min()
    return (df.loc[a, 'date'], df.loc[b, 'date'])

a,b = get_date_range(df, cols=['A', 'B'])
a
#Timestamp('2022-01-03 00:00:00')
b
#Timestamp('2022-01-04 00:00:00')

现在对数据进行筛选:
df.loc[(df.date >= a) & (df.date <= b)]
#        date    A    B    C
#2 2022-01-03  3.0  3.0  3
#3 2022-01-04  4.0  4.0  4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接