简体   繁体   中英

Pandas, substract columns Dataframe in loop

I am new with pandas. I have a Dataframe that consists in 6 columns and I would like to make a for loop that does this:

-create a new column (nc 1) -nc1 = column 1 - column 2

and I want to iterate this for all columns, so the last one would be: ncx = column 5- column 6

I can substract columns like this:

df['nc'] = df.Column1 - df.Column2

but this is not useful when I try to do a loop since I always have to insert the names of colums.

Can someone help me by telling me how can I refer to columns as numbers? Thank you!

In [26]: import numpy as np
    ...: import random
    ...: import pandas as pd
    ...: 
    ...: A = pd.DataFrame(np.random.randint(100, size=(5, 6)))

In [27]: A
Out[27]: 
    0   1   2   3   4   5
0  82  13  17  58  68  67
1  81  45  15  11  20  63
2   0  84  34  60  90  34
3  59  28  46  96  86  53
4  45  74  14  10   5  12

In [28]: for i in range(0, 5):
    ...:     A[(i + 6)] = A[i] - A[(i + 1)]
    ...: 
    ...: 
    ...: A
    ...: 
Out[28]: 
   0   1   2   3   4   5   6   7   8   9   10
0  82  13  17  58  68  67  69  -4 -41 -10   1
1  81  45  15  11  20  63  36  30   4  -9 -43
2   0  84  34  60  90  34 -84  50 -26 -30  56
3  59  28  46  96  86  53  31 -18 -50  10  33
4  45  74  14  10   5  12 -29  60   4   5  -7

In [29]: nc = 1 #The first new column
    ...: A[(nc + 5)] #outputs the first new column
Out[29]: 
0    69
1    36
2   -84
3    31
4   -29

Here you don't need to call it by name, just by the column number, and you can just write a simple function that calls the column + 5

Something like this:

In [31]: def call_new_column(n):
    ...:     return(A[(n + 5)])
    ...: 
    ...: 
    ...: call_new_column(2)
Out[31]: 
0    -4
1    30
2    50
3   -18
4    60

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