Answers for "use iloc to loop through dataframe"

0

use iloc to loop through dataframe

vals = [] # Create an empty list to hold the requested values
for i in range(len(df['loc'])): # Loop over the rows ('i')
    val = df.iloc[i, df['loc'][i]] # Get the requested value from row 'i'
    vals.append(val) # append value to list 'vals'
df['value'] = vals # Add list 'vals' as a new column to the DataFrame
Posted by: Guest on January-23-2022

Code answers related to "use iloc to loop through dataframe"

Python Answers by Framework

Browse Popular Code Answers by Language