简体   繁体   中英

Iterate over rows and append values to column in dataframe

I am trying to loop through the rows and transform data shown in pic 1 to that of pic 2.

Having trouble in fetching the corresponding Quarter values as the list I get for each of these colors is not in the sequential order. Can you please suggest how can I attain the result of Pic2?

b=[]
r=[]
g=[]
p=[]
for col, col_val in Sample.iteritems():
    for i in col_val.values:
        if 'Blue' in i:
            b.append(i[i.find('(')+1 : i.find('-')])
        elif 'Purple' in i:
            p.append(i[i.find('(')+1 : i.find('-')])
        elif 'Green' in i:
            g.append(i[i.find('(')+1 : i.find('-')])
        elif 'Red' in i:
            r.append(i[i.find('(')+1 : i.find('-')])

图1图2

The issue got fixed when iteritems was replaced by iterrows .

b=[]
r=[]
g=[]
p=[]
q=[]
for col, col_val in Sample.iterrows():
    for i in col_val.values:
        if 'Blue' in i:
            b.append(pd.to_numeric(i[i.find('(')+1 : i.find('-')]))
        elif 'Purple' in i:
            p.append(pd.to_numeric(i[i.find('(')+1 : i.find('-')]))
        elif 'Green' in i:
            g.append(pd.to_numeric(i[i.find('(')+1 : i.find('-')]))
        elif 'Red' in i:
            r.append(pd.to_numeric(i[i.find('(')+1 : i.find('-')]))
        else:
            q.append(i)
pd.DataFrame(list(zip(q,g,p,r,b))).rename(columns={0:'Quarter',1:'Green',2:'Purple',3:'Red',4:'Blue'})

I considered a different approach. I came up with another approach: join the index column and the target column vertically, and then split the column and join it vertically afterwards. I thought it would be easier, but it was fiddly.

df.columns = ['Quarter','value','value','value','value']
df1 = pd.DataFrame()
for i in range(1,len(df.columns)):
    df1 = df1.append(df.iloc[:,[0,i]], ignore_index=True)

df2 = pd.concat([df1[['Quarter']], df1['value'].str.split("(", expand=True)], axis=1)
df2 = pd.concat([df2[['Quarter',0]], df2[1].str.split('-', expand=True)], axis=1)

df2.columns = ['Quarter', 'color', 'value', 'tmp']
df2.drop('tmp', inplace=True, axis=1)

final = pd.DataFrame()
for k in df2['Quarter'].unique():
    tmp = df2[df2['Quarter'] == k]
    tmp = tmp.pivot(index='Quarter',columns='color')
    final = final.append(tmp, ignore_index=True)

final = final.droplevel(0, axis=1)
final['Quarter'] = df2['Quarter'].unique()
final = final[['Quarter','Green','Purple','Red','Blue']]

final
color   Quarter Green   Purple  Red Blue
0   Q2  1   6   4   3
1   Q3  6   10  2   7
2   Q4  3   7   6   2

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM