Home > Net >  How to enter the value of one index and column into a new cell with 1 in the iteration?
How to enter the value of one index and column into a new cell with 1 in the iteration?

Time:04-17

I have the following DataFrame named df1:

col1 col2 col3
5 3 50
10 4 3
2 0 1

I would like to create a loop that adds a new column called "Total", which takes the value of col1 index 0 (5) and enters that value under the column "Total" at index 0. The next iteration, will col2 index 1 (4) and that value will go under column "Total" at index 1. This step will continue all columns and rows are completed.

The ideal output will be the following:

df1

col1 col2 col3 Total
5 3 50 5
10 4 3 4
2 0 1 1

I have the following code but I would like to find a more efficient way of doing this as I have a large DataFrame:

df1.iloc[0,3] = df1.iloc[0,0]
df1.iloc[1,3] = df1.iloc[1,1]
df1.iloc[2,3] = df1.iloc[2,2]

Thank you!

CodePudding user response:

Numpy has a built in diagonal function:

import pandas as pd
import numpy as np
df = pd.DataFrame({'col1': [5, 10, 2], 'col2': [3, 4, 0], 'col3': [50, 3, 1]})
df['Total'] = np.diag(df)

print(df)

Output

   col1  col2  col3  Total
0     5     3    50      5
1    10     4     3      4
2     2     0     1      1

CodePudding user response:

You can try apply on rows

df['Total'] = df.apply(lambda row: row.iloc[row.name], axis=1)
   col1  col2  col3  Total
0     5     3    50      5
1    10     4     3      4
2     2     0     1      1

CodePudding user response:

Hope this logic will help

length = len(df1["col1"])
total = pd.Series([df1.iloc[i, i%3] for i in range(length)])

# in i%3, 3 is number of cols(col1, col2, col3)
# add this total Series to df1 
  • Related