简体   繁体   中英

Parse data frame by rows

I have a data frame that has 5 columns named as '0','1','2','3','4'

small_pd
Out[53]: 
          0     1     2     3     4 
0      93.0  94.0  93.0  33.0   0.0  
1      92.0  94.0  92.0  33.0   0.0 
2      92.0  93.0  92.0  33.0   0.0  
3      92.0  94.0  20.0  33.0  76.0 

I want to use row-wise the input above to feed a function that does the following. I give as example for the first and second row

firstrow:

takeValue[0,0]-takeValue[0,1]+takeValue[0,2]-takeValue[0,3]+takeValue[0,4]

secondrow:

   takeValue[1,0]-takeValue[1,1]+takeValue[1,2]-takeValue[1,3]+takeValue[1,4]

for the third row onwards and then assign all those results as an extra column.

small_pd['extracolumn']

Is there a way to avoid a typical for loop in python and do it in a much better way?

Can you please advice me? Thanks a lot Alex

You can use pd.apply

df = pd.DataFrame(data={"0":[93,92,92,92],
                   "1":[94,94,93,94],
                   "2":[93,92,92,20],
                   "3":[33,33,33,33],
                   "4":[0,0,0,76]})

def calculation(row):
    return row["0"]-row["1"]+row["2"]-row["3"]+row["4"]


df['extracolumn'] = df.apply(calculation,axis=1)
print(df)
    0   1   2   3   4  result
0  93  94  93  33   0      59
1  92  94  92  33   0      57
2  92  93  92  33   0      58
3  92  94  20  33  76      61

Dont use apply , because loops under the hood, so slow.

Get pair and unpair columns by indexing by DataFrame.iloc , sum them and then subtract for vectorized, so fast solution:

small_pd['extracolumn'] = small_pd.iloc[:, ::2].sum(1) - small_pd.iloc[:, 1::2].sum(1)
print (small_pd)
      0     1     2     3     4  extracolumn
0  93.0  94.0  93.0  33.0   0.0         59.0
1  92.0  94.0  92.0  33.0   0.0         57.0
2  92.0  93.0  92.0  33.0   0.0         58.0
3  92.0  94.0  20.0  33.0  76.0         61.0

Verify :

a = small_pd.iloc[0,0]-small_pd.iloc[0,1]+small_pd.iloc[0,2]-
    small_pd.iloc[0,3]+small_pd.iloc[0,4]
b = small_pd.iloc[1,0]-small_pd.iloc[1,1]+small_pd.iloc[1,2]-
    small_pd.iloc[1,3]+small_pd.iloc[1,4]
print (a, b)
59.0 57.0

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