1

I have a dataframe (my_data) like this:

 ID  col1  col2  col3
 100  nan   nan   nan
 101  nan   nan   nan
 102  nan   nan   nan
 103  nan   nan   nan

and I have a list "My_list" with one column and n rows:

[val1
val2
val3
val4]

I want to insert the list with 1 column and n rows into the dataframe in a specific column in a loop. I can not use the name of the column because in a loop the name of the headers will be changed. The output should be like this:

 ID  col1  col2  col3
 100  nan  val1   nan
 101  nan  val2   nan
 102  nan  val3   nan
 103  nan  val4   nan

So I need to use index or something like this. Can anyone help me?

MRza
  • 61
  • 5
  • 1
    Check this out something similar but with rows. https://stackoverflow.com/questions/24284342/insert-a-row-to-pandas-dataframe – PanwarS87 Sep 25 '17 at 17:46

1 Answers1

0

What you are looking for is df.iloc! It allowes you to slice the dataframe by index. To replace the values in a column with index i with the values in the list lst:

df.iloc[:,i] = lst
piiipmatz
  • 400
  • 1
  • 10