0

Previous answers don't help me because I'm looping through dataframes, where the columns change each loop so referencing a specific column df["col"] doesn't work.

I have a large DataFrame that's just a more complicated version of:

data = [[1, 2, 3, 4, 5], [10, 20, 30, 40, 50], [100, 200, 300, 400, 500]]  
df1 = pd.DataFrame(data, columns = ['Time(s)', 'Cell1', 'Cell2'])

I then loop through the cells, which change each time I run the code:

for cell in df1.columns[2:]: 
*do a bunch of stuff looping through other dataframes* 

I end up with two variables, timelower and timeupper, I want every row (inclusive) between these two times into a new dataframe, where the new dataframe has the same columns but the new times between timelower and timeupper.

The desired output is a new dataframe, df2, with the same columns as df1 but only specific rows between timelower and timeupper in df1.

  • 1
    I'm not sure I understand the question. What is the expected output for this example? Do you mean like `iloc` so you can access by integer location instead of by label? – Henry Ecker Oct 27 '21 at 15:17
  • Yes, I want to access the rows between whatever times I have stored in my variable, and save these to a new dataframe with the same columns. – Flynn O'Connell Oct 27 '21 at 15:26
  • This similar question might help you: [How to select rows in a DataFrame between two values, in Python Pandas?](https://stackoverflow.com/questions/31617845/how-to-select-rows-in-a-dataframe-between-two-values-in-python-pandas). In your case, it would be something like `df2 = df1[df1['Time(s)'].between(timelower, timeupper)]` – Leland Hepworth Oct 27 '21 at 17:40

0 Answers0