如何在pandas dataframe中找到最长的连续值字符串

时间:2017-09-12 07:37:25

标签: python pandas dataframe

我希望在我的熊猫df中找到最长的零串。我有一个包含10列的df数组,每列有25000行,具有null,零或非零数字。我想要计算:

 1. A value which states the longest consecutive number 
        of zeros in each column for all the columns. 
 2. A value which states the longest consecutive number 
         of zeros AND nulls in each column for all the columns. 

e.g。如果第一列是:

[col1:1,2,4,5,6,2,3,0,0,0,0,1,2,... (remaining all numbers)]

将返回4.

由于

2 个答案:

答案 0 :(得分:1)

设置

考虑数据框Toolbar

df

解决方案

df = pd.DataFrame(dict(
    col0=[1, 2, 3, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 9],
    col1=[1, 2, 3, 0, 0, 4, 0, 1, 2, 3, 4, 0, 0, 0, 1, 2, 0, 0, 2, 0, 4, 8, 9]
))

答案 1 :(得分:1)

如果你有像

这样的数据框
Alamofire
        .request(
            "https://api.everypixel.com/oauth/token",
            parameters:[
                "client_id"     : clientID,
                "client_secret" : clientSecret,
                "grant_type"    : "client_credentials"
            ]
        )
        .responseString { response in
            switch response.result {
            case .success(let value):
                print("from .success \(value)")
            case .failure(let error):
                print(error)
            }
    }

您可以使用itertools groupby

df = pd.DataFrame([[1, 2, 4, 5, 6, 2, 3, 0, 0, 0 ,0, 1, 2],[1, 0, 0, 2, 0, 2, 0, 0, 0, 0 ,0, 1, 2]])

输出:

  0  1  2  3  4  5  6  7  8  9  10  11  12  max
0  1  2  4  5  6  2  3  0  0  0   0   1   2    4
1  1  0  0  2  0  2  0  0  0  0   0   1   2    5