2

I would like to create a numpy array without creating a list first.
At the moment I've got this:

import pandas as pd
import numpy as np 
dfa = pd.read_csv('csva.csv')
dfb = pd.read_csv('csvb.csv')
pa = np.array(dfa['location'])
pb = np.array(dfb['location'])
ra = [(pa[i+1] - pa[i]) / float(pa[i]) for i in range(9999)]
rb = [(pb[i+1] - pb[i]) / float(pb[i]) for i in range(9999)]
ra = np.array(ra)
rb = np.array(rb)

Is there any elegant way to do in one step the last fill in of this np array without creating the list first ?

Thanks

tema
1,12515 silver badges34 bronze badges
asked Apr 28, 2015 at 9:05
3
  • Few steps might help 1) Post raw data 2) Expected output. Commented Apr 28, 2015 at 9:08
  • You could also use dfa['location'].values in palce of np.array(dfa['location']) Commented Apr 28, 2015 at 10:39
  • Wanted to ask same question, but I see now there is no general answer. Commented Jan 13, 2017 at 12:05

3 Answers 3

4

You can calculate with vectors in numpy, without the need of lists:

ra = (pa[1:] - pa[:-1]) / pa[:-1]
rb = (pb[1:] - pb[:-1]) / pb[:-1]
answered Apr 28, 2015 at 9:09
Sign up to request clarification or add additional context in comments.

Comments

2

The title of your question and what you need to do in your specific case are actually two slighly different things.

To create a numpy array without "casting" a list (or other iterable) you can use one of the several methods defined by numpy itself that returns array:

  • np.empty, np.zeros, np.ones, np.full to create arrays of given size with fixed values
  • np.random.* (where * can be various distributions, like normal, uniform, exponential ...), to create arrays of given size with random values

In general, read this: Array creation routines

In your case, you already have numpy arrays (pa and pb) and you don't have to create lists to calculate the new arrays (ra and rb), you can directly operate on the numpy arrays (which is the entire point of numpy: you can do operations on arrays way faster that would be iterating over each element!). Copied from @Daniel's answer:

ra = (pa[1:] - pa[:-1]) / pa[:-1]
rb = (pb[1:] - pb[:-1]) / pb[:-1]

This will be much faster than you're current implementation, not only because you avoid converting a list to ndarray, but because numpy arrays are order of magnuitude faster for mathematical and batch operations than iteration

answered Apr 28, 2015 at 9:48

Comments

0

numpy.zeros

Return a new array of given shape and type, filled with zeros.

or

numpy.ones

Return a new array of given shape and type, filled with ones.

or

numpy.empty

Return a new array of given shape and type, without initializing entries.

answered Apr 28, 2015 at 9:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.