0

i want to create 120 Objets in the form shown below. But its to long. How would you do it with a loop, so that the code will be shorter?

import numpy as np
import pandas as pd
import scipy.stats as stats

np.df1 = pd.read_csv('C:/Users/.../0Grad.csv', delimiter=';')
np.df_1 = np.df1['Magnitude']
d1 = np.array(np.df_1.iloc[0:10])
d2 = np.array(np.df_1.iloc[10:20])
d3 = np.array(np.df_1.iloc[20:30])
d4 = np.array(np.df_1.iloc[30:40])
d5 = np.array(np.df_1.iloc[40:50])
...
d120 = np.array(np.df_1.iloc[1190:1200])
Lony Sz
  • 1
  • 1
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – quamrana Jul 30 '21 at 16:28
  • Don't use a name like `np.df1`. `np` is a module which you imported. You shouldn't be adding anything to it, Just write `df=pd.read...`. And what you call `df_1` is a Series, a column of the frame. – hpaulj Jul 30 '21 at 22:48

5 Answers5

0

Probably

import numpy as np
import pandas as pd
import scipy.stats as stats

np.df1 = pd.read_csv('C:/Users/.../0Grad.csv', delimiter=';')
np.df_1 = np.df1['Magnitude']
d = []
for i in range(0,1180,10):
    d.append(np.array(np.df_1.iloc[i:i+10]))
d.append(np.array(np.df_1.iloc[1190:2000]))
joanis
  • 10,635
  • 14
  • 30
  • 40
ClickName
  • 101
  • 2
0

you can use a for loop to do this

dx_list = []
for x in range(0,120,10):
   dx = np.array(np.df_1.iloc[x:x+10]) 
   dx_list .append("orange") ...
En Xie
  • 510
  • 4
  • 19
0

You could use a dictionary.

d = {}
for idx in range(0, 121):
    d[f'd{idx}'] = np.array(np.df_1.iloc[(idx*10):(idx*10)+10])

The above code will create a dictionary d which has keys d1, d1, ..., d120.

Note, I'm assuming iloc[1190:2000] is a typo and you actually mean iloc[1990:2000]

norie
  • 9,609
  • 2
  • 11
  • 18
0

You could try something like this:

import numpy as np
import pandas as pd
import scipy.stats as stats

np.df1 = pd.read_csv('C:/Users/.../0Grad.csv', delimiter=';')
np.df_1 = np.df1['Magnitude']

li = []
x = 0
y = 10
counter = 1

while counter <= 120:
    li.append(np.array(np.df_1.iloc[x:y]))
    x += y
    y += y

Then you should have the preferred result in a list.

MonteCarlo
  • 26
  • 4
0
In [213]: import pandas as pd

Make a dataframe, as you do with the file read:

In [214]: df = pd.DataFrame(np.arange(100), columns=['mag'])
In [215]: df
Out[215]: 
    mag
0     0
1     1
2     2
3     3
4     4
..  ...
95   95
96   96
97   97
98   98
99   99

[100 rows x 1 columns]

Select one column, getting a pandas Series:

In [216]: s = df['mag']
In [217]: s
Out[217]: 
0      0
1      1
2      2
3      3
4      4
      ..
95    95
96    96
97    97
98    98
99    99
Name: mag, Length: 100, dtype: int64

THe series as a numpy array (or array derived from the series):

In [218]: s.to_numpy()
Out[218]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

Use np.split to divide the array in 10 equal sized arrays. The result is a list. No need to assign them individually to variables.

In [219]: np.split(s.to_numpy(),10)
Out[219]: 
[array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
 array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
 array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29]),
 array([30, 31, 32, 33, 34, 35, 36, 37, 38, 39]),
 array([40, 41, 42, 43, 44, 45, 46, 47, 48, 49]),
 array([50, 51, 52, 53, 54, 55, 56, 57, 58, 59]),
 array([60, 61, 62, 63, 64, 65, 66, 67, 68, 69]),
 array([70, 71, 72, 73, 74, 75, 76, 77, 78, 79]),
 array([80, 81, 82, 83, 84, 85, 86, 87, 88, 89]),
 array([90, 91, 92, 93, 94, 95, 96, 97, 98, 99])]

If you don't understand the logic of a list of arrays instead of assignment to individual variables, you need to read some more basic Python.

hpaulj
  • 221,503
  • 14
  • 230
  • 353