【问题标题】:getting NAN values from for loop [python pandas]从 for 循环 [python pandas] 获取 NAN 值
【发布时间】:2021-05-26 05:00:16
【问题描述】:

我有一个带有 CREATIVE_NAME 列的 python 数据框,我想通过搜索特定的子字符串来创建一个新列 CREATIVE_SIZE 并将它们放入新列中。

 creative_size = []
    for i in df['CREATIVE_NAME']:
        if search('320x480', i):
            creative_size.append('320x480')
        elif search('728x1024', i):
            creative_size.append('728x1024')
        elif search('320x50', i):
            creative_size.append('320x50')
        elif search('728x90', i):
            creative_size.append('728x90')
        elif search('300x250', i):
            creative_size.append('300x250')
        elif search('80x80', i):
            creative_size.append('80x80')
        elif search('1200x627', i):
            creative_size.append('1200x627')
        elif search('768x1024', i):
            creative_size.append('768x1024')
        elif search('320x420', i):
            creative_size.append('320x420')
        elif search('768x820', i):
            creative_size.append('768x820')
        else:
            creative_size.append('no creative size')

sizes = pd.Series(creative_size)
df.insert(column='creative_size', value=sizes, loc = 0)

df['creative_size'].isna().sum()
output: 1579

我不明白为什么我要从 for 循环中获取 NAN 值,因为它应该已经捕获了所有条件并且不应该遗漏任何内容。

【问题讨论】:

    标签: python pandas loops for-loop na


    【解决方案1】:
    import pandas as pd
    ####    FOR TESTING ####
    test_data_dict = {
    'CREATIVE_NAME':['320x480', '728x1024', '1000x1000']
    }
    
    df = pd.DataFrame(data=test_data_dict)
    
    #### Define a set of all creative sizes you want to check against
    
    creative_sizes =('320x480','728x1024','320x50','728x90','300x250','80x80','1200x627','768x1024','320x420','768x820') #list of valid creative sizes
    
    ###### Define a function which will check if `C_name` is a substring of available creative_sizes
    
    def get_creative_size(c_name):
        #c_name is the value of creative_name in row
        result = [size for size in creative_sizes if c_name in size] 
        if len(result) > 0:
            return result[0]
        else:
            return 'no creative size'
    
    df['CREATIVE_SIZE'] = df['CREATIVE_NAME'].apply(lambda x: get_creative_size(x))
    print(df.head())
    

    【讨论】:

    • 嘿,按照你的代码,我每行都“没有创意尺寸”
    • 这个问题可以通过添加细节变得更棒。请查看stackoverflow.com/help/how-to-ask 并相应地编辑您的帖子。
    猜你喜欢
    • 2013-04-20
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    相关资源
    最近更新 更多