OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Filling DataFrame columns by index range

  • Thread starter Thread starter Grufoony
  • Start date Start date
G

Grufoony

Guest
Suppose to have two lists, namely indexes and values of the same lenght. I want to add a column to an existing DataFrame, e.g. df["Values"] using these lists, in particular by assigning values[0] to all rows with index bewteen 0 and indexes[0], then values[1] to all rows with index bewteen indexes[0] and indexes[1] and so on...

At this point I was only able to get this result by iterating over all the dataframes with the df.iterrows() method, but this is rather slow when the size of the dataframe (csv) is about 20 MB. Is there an elegant way to get the same result without using a for loop but a pandas method or similar?
<p>Suppose to have two lists, namely <code>indexes</code> and <code>values</code> of the same lenght.
I want to add a column to an existing DataFrame, e.g. <code>df["Values"]</code> using these lists, in particular by assigning <code>values[0]</code> to all rows with index bewteen <code>0</code> and <code>indexes[0]</code>, then <code>values[1]</code> to all rows with index bewteen <code>indexes[0]</code> and <code>indexes[1]</code> and so on...</p>
<p>At this point I was only able to get this result by iterating over all the dataframes with the <code>df.iterrows()</code> method, but this is rather slow when the size of the dataframe (csv) is about 20 MB.
Is there an elegant way to get the same result without using a for loop but a pandas method or similar?</p>
 

Latest posts

Top