【问题标题】:Iterating over conditions from columns and Dataframe to list conversion(pandas)从列和数据框迭代条件到列表转换(熊猫)
【发布时间】:2021-07-26 13:41:10
【问题描述】:

我有一个这样的数据框:

Item   Quantity  Price     Photo1     Photo2    Photo3    Photo4

A        2         30      A1.jpg      A2.jpg 
B        4         10      B1.jpg      B2.jpg    B3.jpg    B4.jpg
C        5         15      C1.jpg

这些是我之前关于以这种格式引入数据框的问题。

How to split datas from columns and add to a list from a dataframe, also repeat the list elements for a single row? (Pandas)

我首先创建了一个列表:

df1 = df.reindex(['Item','Quantity','Price','Photo1','Photo2','Photo3','Photo4','I','Q','P','PH',] axis=1)
df1['I'] = df1['I'].fillna['I']
df1['Q'] = df1['Q'].fillna['Q']
df1['P'] = df1['P'].fillna['P']
df1['PH'] = df1['PH'].fillna['PH']
vals = [['I','Item'],['Q','Quantity'],['P','Price']]

我从第一个问题开始尝试:

photo_df = df1.fillna('').filter(like='Photo')


vals = [y for x in photo_df.to_numpy() 
         for y in vals[:3] + [['PH',z] for z in x[x!='']] ]

列表返回

vals = [['I','Item'],['Q','Quantity'],['P','Price'],['PH','A1.jpg'],['PH','A2.jpg'],
        ['I','Item'],['Q','Quantity'],['P','Price'],['PH','B1.jpg'],['PH','B2.jpg'],['PH','B3.jpg'],['PH','B4.jpg'],
        ['I','Item'],['Q','Quantity'],['P','Price'],['PH','C1.jpg']]

我希望列表为:

vals = [['I','Item'],['Q','Quantity'],['P','Price'],['PH','Photo1'],['PH','Photo2'],
        ['I','Item'],['Q','Quantity'],['P','Price'],['PH','Photo1'],['PH','Photo2'],['PH','Photo3'],['PH','Photo4'],
        ['I','Item'],['Q','Quantity'],['P','Price'],['PH','Photo1']]
   

我想将标题名称保留在列表中而不是数据中,但应该以问题中的格式迭代数据: How to split datas from columns and add to a list from a dataframe, also repeat the list elements for a single row? (Pandas)

【问题讨论】:

    标签: python python-3.x pandas dataframe numpy


    【解决方案1】:

    想法是过滤列名称而不是列表理解中的值 - 将 x[x!=''] 更改为 photo_df.columns[x!='']

    vals = [y for x in photo_df.to_numpy() 
              for y in vals[:3] + [['PH',z] 
              for z in photo_df.columns[x!='']]]
    print (vals)
    [['I', 'Item'], ['Q', 'Quantity'], ['P', 'Price'], ['PH', 'Photo1'], ['PH', 'Photo2'], 
     ['I', 'Item'], ['Q', 'Quantity'], ['P', 'Price'], ['PH', 'Photo1'], ['PH', 'Photo2'], ['PH', 'Photo3'], ['PH', 'Photo4'], 
     ['I', 'Item'], ['Q', 'Quantity'], ['P', 'Price'], ['PH', 'Photo1']]
    

    【讨论】:

    【解决方案2】:

    您可以像这样在创建photo_df 的地方做一个小改动:

    photo_df = df1.filter(like='Photo')
    photo_df = photo_df.transform(lambda x: np.where(x.isnull(), x, x.name)) 
    photo_df = photo_df.fillna('')
    

    第二行只是将非空值替换为其列名。

    输出:

    [['I', 'Item'], ['Q', 'Quantity'], ['P', 'Price'], ['PH', 'Photo1'], ['PH', 'Photo2'], 
    ['I', 'Item'], ['Q', 'Quantity'], ['P', 'Price'], ['PH', 'Photo1'], ['PH', 'Photo2'], 
    ['PH', 'Photo3'], ['PH', 'Photo4'], ['I', 'Item'], ['Q', 'Quantity'], ['P', 'Price'], ['PH', 'Photo1']]
    

    【讨论】:

    猜你喜欢
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多