Struggling to make a data slice

Hello all!

I’m very new to python and programming in general. Looking for some assistance.

I’m using a dataframe and trying to make slices of data that are a fixed length in sample size.

I have made my start point and end point, but am not sure how to create a slice using those points and then export to .CSV

The line I’m trying to run to make this work is:

data_slice = data.loc[num1:num2]

Is this correct for what it trying to do?


Welcome, Adam.

data_slice = data.loc[num1:num2]

Is fine as long as…

  1. …data_slice is a subscriptable, sliceable data object.
  2. num1 and num2 are index locations within the data object.
        > otherwise you get an invisible None result.
  3. …all is aligned in the Pandas world (you mentioned “dataframe”)

With so little code and no context, it’s impossible to say WHY you aren’t getting a result. All anyone can really do is assure you that your syntax looks okay, but even that isn’t a given because of ~.loc[]. I’m 99.999% sure that’s a Pandas dataframe method and your best bet for that part is to try a dedicated Pandas forum.

BTW, paste your code between three ‘backticks’ to make it easy for people to understand what you’re presenting. That’s how the code line above was done.

<code goes here>


Thanks for getting back to me. I’m going to try to make sure your points are met in the rest of the code and get back to you.


Okay. Here are a few helpful points for when you’re ready to present your progress:

  1. Show your input data
  2. Show your output data
  3. Show the output you expect
  4. IMPORTANT: paste the complete text of any error messages you get
    …and fence the error text in backticks like this:

<code goes here>
You can also use inline monospace by enclosing the text in single backticks.

`use inline monospace`

Hey Leland Parker.

Your steps helped with that part and got it working. Thank you very much.

You’re very welcome, Adam. You mentioned “dataframe”, so it sounds like you are using Pandas. Is that true?

Sometimes (many times) a simple process can be done easily in native Python without even importing from the Standard Library (stdlib). Pandas if for processing large amounts of data. If the job is simple, it can be like driving a bus to go to the grocery store and buy milk.

If your project is part of learning Pandas, then of course you should do it in Pandas.

I’m very new to python and programming in general.

Do you need to use a dataframe or will a simpler data structure work for your application? You will learn more if you understand the code you create.