I have a dataframe like this:
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? (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? (Pandas)
You can just make a small change where you create the photo_df
like this:
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:
[['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!='']
:
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']]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.