I want to make some modifications to my previous Question:
Iterating over conditions from columns and Dataframe to list conversion(pandas)
The dataframe is:
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
I tried:
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.filter(like='Photo')
photo_df = photo_df.transform(lambda x: np.where(x.isnull(), x, x.name))
photo_df = photo_df.fillna('')
vals = [y for x in photo_df.to_numpy()
for y in vals[:3] + [['PH',z] for z in x[x!='']] ]
vals returns:
[['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']]
Now I want to fill in the values from the previous data frame:
I tried:
L = [df1.loc[:, x].set_axis(range(len(x)), axis=1) for x in vals]
This returned in the format:
[I,A,I,B,I,C,Q,2,Q,4,Q,5....................]
I want the L as:
[I,A,Q,2,P,30,PH,A1.jpg,PH,A2.jpg,I,B..............]
Expected dataframe:
I A
Q 2
P 4
PH A1.jpg
PH A2.jpg
I B
Q 4
P 10
PH B1.jpg
PH B2.jpg
PH B3.jpg
PH B4.jpg
I C
Q 5
P 15
PH C1.jpg
See Question&Answers more detail:
os