[英]Iterating over conditions from columns and Dataframe to list conversion(pandas)
I have a dataframe like this:我有一个像这样的 dataframe:
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
These are my previous questions related to bringing the data frame in this format.这些是我以前与以这种格式引入数据框有关的问题。
How to split datas from columns and add to a list from a dataframe, also repeat the list elements for a single row? 如何从列中拆分数据并从 dataframe 添加到列表中,还重复单行的列表元素? (Pandas)
(熊猫)
I first created a list:我首先创建了一个列表:
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']]
I tried from the first question:我从第一个问题开始尝试:
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!='']] ]
the list returns列表返回
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']]
I want the list as:我希望列表为:
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']]
I want to keep the header names in the list instead of the data but should iterate over data in the format from the question: How to split datas from columns and add to a list from a dataframe, also repeat the list elements for a single row?我想将 header 名称保留在列表中而不是数据中,但应该以问题的格式迭代数据: How to split datas from columns and add to a list from a dataframe,还重复单行的列表元素? (Pandas)
(熊猫)
You can just make a small change where you create the photo_df
like this:您可以像这样在创建
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('')
The second line just replaces the non-null value with its column name.第二行只是将非空值替换为其列名。
Output: Output:
[['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']]
Idea is filter columns names instead values in list comprehension - changed x[x!='']
to photo_df.columns[x!='']
:想法是过滤列名称而不是列表理解中的值 - 将
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']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.