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

Pandas dataframe get location of col if name contains string and slice into multiple dataframes

  • Thread starter Thread starter Brain_overflowed
  • Start date Start date
B

Brain_overflowed

Guest
I am reading a .csv that has multiple time series columns, but each has a different name based on packets. My goal is to find the col names that contain the string 'TIME' and get their col numbers so that I can slice the df into multiple dfs with cols beginning with the time series col and ending before the next time series col.

I can get a list of the col names but I am not being able to get their locs in the original df:

Code:
time_cols = [col for col in df.columns if 'TIME' in col]
time_cols_loc = df.columns.get_loc(time_cols)

The above gives an InvalidIndexError, I am assuming because it is extracting that list instead of finding the values in the original df. I am also not sure how to slice it afterwards.
<p>I am reading a .csv that has multiple time series columns, but each has a different name based on packets. My goal is to find the col names that contain the string 'TIME' and get their col numbers so that I can slice the df into multiple dfs with cols beginning with the time series col and ending before the next time series col.</p>
<p>I can get a list of the col names but I am not being able to get their locs in the original df:</p>
<pre><code>time_cols = [col for col in df.columns if 'TIME' in col]
time_cols_loc = df.columns.get_loc(time_cols)
</code></pre>
<p>The above gives an <code>InvalidIndexError</code>, I am assuming because it is extracting that list instead of finding the values in the original df. I am also not sure how to slice it afterwards.</p>
 

Latest posts

Top