Python数据框按行的最小值和最大值(含NaN值)

时间:2020-08-13 02:52:32

标签: python pandas numpy dataframe minmax

我的数据框具有nan。但是我试图找到明智的最小和最大。我怎么找到它。

   func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == messageCollectionView {
            let contentInset = scrollView.adjustedContentInset
            if scrollView.contentOffset.y <= -contentInset.top && !isLoading {
                self.loadMoreData(state: "Loading Previous")
            } else if (scrollView.contentSize.height - scrollView.frame.height <= scrollView.contentOffset.y)   && !isLoading {
                self.loadMoreData(state: "Loading Next")
            }
  
        }
    }
    
    func loadMoreData( state : String  ) {
        if !self.isLoading {
            self.isLoading = true
            DispatchQueue.global().async {
                // Fake background loading task for 2 seconds
                sleep(2)
                // Download more data here
                DispatchQueue.main.async {
                    self.showToast(message: state)
                    self.isLoading = false
                }
            }
        }
    }
    

我正试图在每一行中找到最小值和最大值,并在列df = pd.DataFrame({"A":[12,NaN,13],"B":[45,12,65],"C":[63,78,NaN]}) df= A B C 0 12 45 63 1 NaN 12 78 2 13 65 NaN AB中找到它们之间的差异。 我的代码:

C

当前输出:

poacols = ['A','B','C']
df['dif'] = np.nanmax(df[poacols])-np.nanmin(df[poacols])

预期输出:

df['dif'] = 
0    66
1    66
2    66

1 个答案:

答案 0 :(得分:2)

我们应该添加轴= 1,检查每行的最小和最大

np.nanmax(df[poacols],1)-np.nanmin(df[poacols],1)
Out[81]: array([51., 66., 52.])