Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - add value at specific iloc into new dataframe column

I have a large dataframe containing lots of columns.

For each row/index in the dataframe I do some operations, read in some ancilliary ata, etc and get a new value. Is there a way to add that new value into a new column at the correct row/index?

I can use .assign to add a new column but as I'm looping over the rows and only generating the data to add for one value at a time (generating it is quite involved). When it's generated I'd like to immediately add it to the dataframe rather than waiting until I've generated the entire series.

This doesn't work and gives a key error:

df['new_column_name'].iloc[this_row]=value 

Do I need to initialise the column first or something?

like image 862
Rob Avatar asked Sep 08 '17 09:09

Rob


People also ask

How do you add a value to a specific column in a DataFrame?

And you can use the insert() function to add a new column to a specific location in a pandas DataFrame: df. insert(position, 'col_name', [value1, value2, value3, ...])

How do I add a column to a DataFrame using iLOC?

1: By declaring a new list as a column. df['New_Column']='value' will add the new column and set all rows to that value. In this example, we will create a dataframe df and add a new column with the name Course to it. "A value is trying to be set on a copy of a slice from a DataFrame" .

How do I assign a value to a new column in pandas?

To assign new columns to a DataFrame, use the Pandas assign() method. The assign() returns the new object with all original columns in addition to new ones. Existing columns that are re-assigned will be overwritten. The length of the newly assigned column must match the number of rows in the DataFrame.


1 Answers

There are two steps to created & populate a new column using only a row number... (in this approach iloc is not used)

First, get the row index value by using the row number

rowIndex = df.index[someRowNumber] 

Then, use row index with the loc function to reference the specific row and add the new column / value

df.loc[rowIndex, 'New Column Title'] = "some value" 

These two steps can be combine into one line as follows

df.loc[df.index[someRowNumber], 'New Column Title'] = "some value" 
like image 74
RumbleFish Avatar answered Sep 18 '22 08:09

RumbleFish