0

I'm working with arrays of length 1,000-10,000 elements, which all have the same length.

I need to return an array (called "points" in the sample code below) which contains a dictionary in each element of the array.

I have been looking for similar methods to the .loc method that can be used to create a rule for a column in a pandas data frame, but still haven't found a solution that doesn't involve iteratively appending the array I need to return.

Solutions greatly appreciated!

Current Code:

times = [000000001,000034000,...]
highs = [2,1,...]
lows = [-2,-3,...]

points = []
for i in range(0, len(times)):
    points.append({"time": times[i], "low": lows[i], "high": highs[i]})

Solutions Suggested by Comments:

import pandas as pd
df = pd.DataFrame({"time": times,"low":lows,"high":highs})

which looks like:

Time    High    Low
0   100000001   1   -1
1   100000002   3   -4
2   100000003   4   -5

SE Posts Already Consulted:

Iteratively appending ndarray arrays using numpy in Python

Alternative/faster ways of creating arrays in python 2.7

iterate list of dictionary and append python

jros
  • 387
  • 2
  • 15
  • 2
    Turning those 3 arrays into a dataframe where each point is a row wouldn't work for you? – Juan C Aug 19 '21 at 19:16
  • Agreed, sounds like you need a pandas DataFrame here. – jfaccioni Aug 19 '21 at 19:22
  • @JuanC I've got them in a dataframe (added to post), but I'm stuck on how to put a dictionary together "row by row" without iterating – jros Aug 19 '21 at 19:41
  • @jros Can you explain why do you want a dictionary for each row? That's a very inefficient representation. With pandas and numpy, you work around Python's slowness by keeping a small number of arrays instead of lots of pure Python objects. It's common (and good) for libraries to make inefficient things inconvenient to type. If you really need that representation, then you'll just have to write a loop. There can be good reasons for wanting it, but it's good to check that it's really required first. – Mr Fooz Aug 19 '21 at 19:49
  • @MrFoozm the dictionary formatting is solely because it needs to be outputted in json format – jros Aug 19 '21 at 20:10

1 Answers1

1

You could make it roughly 40% faster using a list comprehension:

points = [{"time":T,"low":L,"high":H} for T,L,H in zip(times,lows,highs)]
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • I've upvoted this answer because it did shave some time off (as you said, probably about 40%), would like to see other possible solutions before accepting – jros Aug 19 '21 at 20:24