简体   繁体   English

找到第一个非空和非空字符串值

[英]find first non-null & non-empty string value

I was using this to find the first non null value of a string:我用它来查找字符串的第一个非空值:

def get_first_non_null_values(df):
    first_non_null_values = []
    try:
        kst = df['kst'].loc[df['kst'].first_valid_index()]
        first_non_null_values.append(kst)
    except:
        kst = df['kst22'].loc[df['kst22'].first_valid_index()]
        first_non_null_values.append(kst)
    return first_non_null_values


first_non_null_values = get_first_non_null_values(df_merged)

This worked but now in my new dataset, I have some null values and some "" empty strings.这有效,但现在在我的新数据集中,我有一些空值和一些""空字符串。 How can I modify this such that I can extract the first value which is neither null not an empty string如何修改它,以便我可以提取第一个值,它既不是 null 也不是空字符串

You can use a combination of notnull / astype(bool) and idxmax :您可以使用notnull / astype(bool)idxmax

(df['col'].notnull()&df['col'].astype(bool)).idxmax()

Example input:示例输入:

>>> df = pd.DataFrame({'col': ['', float('nan'), False, None, 0, 'A', 3]})
>>> df
     col
0       
1    NaN
2  False
3   None
4      0
5      A
6      3

output: 5输出: 5

null and truthy states: null 和 truthy 状态:

     col  notnull  astype(bool)   both
0            True         False  False
1    NaN    False          True  False
2  False     True         False  False
3   None    False         False  False
4      0     True         False  False
5      A     True          True   True
6      3     True          True   True

first non empty string value:第一个非空字符串值:

If you're only interesting in strings that are not empty:如果您只对非空字符串感兴趣:

df['col'].str.len().gt(0).idxmax()

I think u need:我认为你需要:

df = pd.DataFrame({'col': ['', np.nan, '', 1, 2, 3]})
print(df['col'].loc[df['col'].replace('', np.nan).first_valid_index()])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM