I have a modified data frame from the previous question: How to fill the values in the list and convert it into the dataframe?
Item Photo1 Photo2 Photo3 Photo4 Description1 Description2 Description 3
A A1.jpg A2.jpg Nice Beautiful
B B1.jpg B2.jpg B3.jpg B4.jpg Ugly Damaged
C C1.jpg Cute Handsome Nice
I want to use the code from the previous question on the two fields:
photo_df= df1.fillna('').filter(like='Photo')
vals = [(i, y) for i, x in enumerate(photo_df.to_numpy())
for y in vals[:3] + [['PH',z]
for z in photo_df.columns[x!='']]]
What i tried is:
photo_df= df1.fillna('').filter(like='Photo')
product_df = df1.fillna('').filter(like='Description')
vals = [(i, y) for i, x in enumerate(photo_df.to_numpy())
for y in vals[:3] + [['PH',z]
for z in photo_df.columns[x!='']]]
vals = [(i, y) for i, x in enumerate(product_df.to_numpy())
for y in vals[:4] + [['Description',z]
for z in product_df.columns[x!='']]]
L = [df1.loc[df1.index[[i]], x].set_axis(range(len(x)), axis=1) for i, x in vals]
df = pd.concat(L)
but this gives the list out of the list,
Again I tried, I divided the dataframes into two parts:
photo_df= df1.fillna('').filter(like='Photo')
photo_df = photo_df.fillna('')
product_df = df1.fillna('').filter(like='Description')
product_df = product_df.fillna('')
vals = [(i, y) for i, x in enumerate(photo_df.to_numpy())
for y in vals[:3] + [['PH',z]
for z in photo_df.columns[x!='']]]
vals2 = [(i, y) for i, x in enumerate(product_df.to_numpy())
for y in vals[:0] + [['Description',z]
for z in product_df.columns[x!='']]]
L = [df1.loc[df1.index[[i]], x].set_axis(range(len(x)), axis=1) for i, x in vals]
M = [df2.loc[df2.index[[i]], x].set_axis(range(len(x)), axis=1) for i, x in vals2]
df = pd.concat(L)
df3 = pd.concat(M)
df = pd.concat([df,df3])
Output:
I A
PH A1.jpg
PH A2.jpg
I B
PH B1.jpg
PH B2.jpg
PH B3.jpg
PH B4.jpg
I C
PH C1.jpg
Description Nice
Description Beauiful
Description Ugly
Description Damaged
Description Cute
Description Handsome
Description Nice
How to properly concatenate in the below format?
Expected Dataframe is similar like to the previous question, with description added.
Expected Output:
I A
PH A1.jpg
PH A2.jpg
Description Nice
Description Beauiful
I B
PH B1.jpg
PH B2.jpg
PH B3.jpg
PH B4.jpg
Description Ugly
Description Damaged
I C
PH C1.jpg
Description Cute
Description Handsome
Description Nice
from How to fill the values in the list and convert it into the dataframe?(Part II)
No comments:
Post a Comment