Akash
Akash

Reputation: 359

Creating a new column after every iteration of For loop in Python

I want to create a column and assign it to a dataframe after every iteration of the for loop in python.

df_xyz = pd.DataFrame()
for j in range(0,3):
    for k in range(j+1,4):
        print(j,k)

So, in this case it should create 6 new columns in the dataframe with the name as "ABC1","ABC2"..."ABC6". And the columns will get the values from a numpy array which is generated by running the code present in the loop. My actual code involves some algorithm but here I am just placing the relevant code on which I need help.

Edit 1:

Updated the code:

z= np.array([1,2,4])
df_xyz = pd.DataFrame()
for j in range(0,3):
    for k in range(j+1,4):
        print(j,k)
        df_xyz = pd.DataFrame(z)  

This creates a new column only once.

Upvotes: 1

Views: 17046

Answers (2)

nick
nick

Reputation: 366

# importing pandas
import pandas as pd

# Creating new dataframe
initial_data = {'First_name': ['Ram', 'Mohan', 'Tina', 'Jeetu', 'Meera'], 
            'Last_name': ['Kumar', 'Sharma', 'Ali', 'Gandhi', 'Kumari'], 
            'Marks': [12, 52, 36, 85, 23] }

df = pd.DataFrame(initial_data, columns = ['First_name', 'Last_name', 
'Marks'])

# Generate result using pandas
result = []
for value in df["Marks"]:
if value >= 33:
    result.append("Pass")
elif value < 0 and value > 100:
    result.append("Invalid")
else:
    result.append("Fail")
   
df["Result"] = result   
print(df)

Upvotes: 1

Benoit Drogou
Benoit Drogou

Reputation: 969

It really looks awful, but I think you are trying to do this:

In [1]:
import pandas as pd 
import numpy as np

z= np.array([1,2,4])
df_xyz = pd.DataFrame()
iterator = 1
for j in range(0,3):
    for k in range(j+1,4):
        print(j,k)
        col_name = 'ABC' + str(iterator)
        df_xyz.loc[:, col_name] = z  
        iterator += 1 
df

Out [1]:
    ABC1    ABC2    ABC3    ABC4    ABC5    ABC6
0   1       1       1       1       1       1
1   2       2       2       2       2       2
2   4       4       4       4       4       4

You can as well do something like this :

In [2]:
import pandas as pd 
import numpy as np

my_cols = ['ABC1', 'ABC2', 'ABC3', 'ABC4', 'ABC5', 'ABC6']

z= np.array([1,2,4])
df_xyz = pd.DataFrame()
for j in range(0,3):
    for k in range(j+1,4):
        print(j,k)
        col_name = my_cols[0]
        my_cols.pop(0)
        df_xyz.loc[:, col_name] = z  
df

Out [2]:
    ABC1    ABC2    ABC3    ABC4    ABC5    ABC6
0   1       1       1       1       1       1
1   2       2       2       2       2       2
2   4       4       4       4       4       4

Upvotes: 3

Related Questions