Installation Program_Execution Data_Structures Data_Input Data_Handling Functions Data_Manipulation

Goal

To clarify the difference between pandas DataFrames (pd.DataFrame) and numpy arrays (np.array).

Additional Information

Here you can find additional information on ...


First Steps with pandas

  • Generate a sine- and a cosine-wave, with
    • amplitude: 2
    • frequency: 0.5 Hz
    • time from 0 to 10 sec, with a sample rate of 10 Hz
    Tips:
    • First, write down the equation on a sheet of paper, and indicate all the variables that need to be defined.
    • For the calculation of the data, the following NumPy-functions can be useful: np.pi (for the value of "pi"), and np.arange (for generating a vector).
  • Inspect the first and the last time value. Is the last value the one you expected?
  • Use these data to create a Python dictionary, with keys ['time', 'x', 'y'], and the values from the corresponding generated data (i.e. 'x' for the sine, and 'y' for the cosine).
  • With this dictionary create a pandas DataFrame.
  • Show the head and the tail of this DataFrame.

Extracting Values ...

... from NumPy arrays

  • Using the values-method of that data-frame, extract the data from the 5th row up to and including the 10th row, from the "x" and "y" columns, using the numpy syntax for "slicing" (e.g. selecting data from a matrix). WARNING: Python starts indexing with "0"!
  • Python
    Python indexing starts from 0. And it is easiest to think of the indices as pointers. E.g. [2:5] gives you the numbers between the pointers 2 and 5, i.e. 5-2=3 values.

... and from DataFrames

Python
DataFrames have their background in databases, and use quite a differnt syntax than NumPy arrays. But they form the backbone for convenient data input and manipulation, and are the basis of Machine Learning in Python. So please invest the time and get familiar with them: your rewards will be plenty!
  • Do the same thing, working directly with the pandas DataFrame. (WARNING: This is harder than it looks - the different syntax in numpy and pandas confuses most newcomers!!)
  • Write the selected data to the file out.txt.
  • Let the user know where the data have been written to.

Solution