Python Forum

Full Version: How to create 2 dimensional variables in Python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import time
import pandas

df = pandas.read_csv('abc.csv')
start = time.time()

i = 0
ii = 0
for ii in range(1, 25):

    for i in range(1419, 2060):
        if df.iloc[i + ii, 2] == df.iloc[i, 3] or df.iloc[i + ii, 2] == df.iloc[i, 4] \
                or df.iloc[i + ii, 2] == df.iloc[i, 5] or df.iloc[i + ii, 2] == df.iloc[i, 6]:
            tracker1(i, ii) = tracker1(i, ii) + 1
        if df.iloc[i + ii, 3] == df.iloc[i, 2] or df.iloc[i + ii, 3] == df.iloc[i, 4] \
                or df.iloc[i + ii, 3] == df.iloc[i, 5] or df.iloc[i + ii, 3] == df.iloc[i, 6]:
            tracker2(i, ii) = tracker2(i, ii) + 1

print(time.time() - start)
I need help on lines 14 and 17 to create 2-dimensional variables tracker1 and tracker2. At a loss on how to do this in Python. In Fortran & VB, straight forward, just declare the variables eg tracker1(100,100).

Thanks.
Please provide a small, sample of abc.csv, large enough and complete enough to be able to run your snippet.
As is common in Python, there are multiple ways to approach the problem.
First, a list of lists: tracker1[i][ii] and use the list functions to manipulate values.
Second, you are using Pandas dataframes. Those ARE 2 dimensional variables. Different syntax.
Third, and this may be closest to your Fortran history, use a Numpy array. That allows you to predefine the size and initialize the array
import numpy as np
tracker1 = np.zeros([100,100],order = "F")
This initializes a 100x100 array of 0's and stores them in Fortran order (alternative is "C" order which is the default).
https://numpy.org/doc/stable/reference/g...zeros.html
(Mar-30-2022, 11:09 AM)Larz60+ Wrote: [ -> ]Please provide a small, sample of abc.csv, large enough and complete enough to be able to run your snippet.

Thanks. The csv file as requested.
(Mar-30-2022, 11:16 AM)jefsummers Wrote: [ -> ]As is common in Python, there are multiple ways to approach the problem.
First, a list of lists: tracker1[i][ii] and use the list functions to manipulate values.
Second, you are using Pandas dataframes. Those ARE 2 dimensional variables. Different syntax.
Third, and this may be closest to your Fortran history, use a Numpy array. That allows you to predefine the size and initialize the array
import numpy as np
tracker1 = np.zeros([100,100],order = "F")
This initializes a 100x100 array of 0's and stores them in Fortran order (alternative is "C" order which is the default).
https://numpy.org/doc/stable/reference/g...zeros.html
Noted and thanks.

I will try it out and see.
I tried the np portion and it runs. I am confused, how does it know which one to use

(i,ii) or (ii,i)?

Thanks.
Not sure I understand the question, but you give it the indexes and it gives you the value
tracker1[25,26] += 1
(Mar-30-2022, 02:41 PM)jefsummers Wrote: [ -> ]Not sure I understand the question, but you give it the indexes and it gives you the value
tracker1[25,26] += 1

Thanks.

Maybe better for me to illustrate what I am after with a Fortran like example.
[attachment=1683]

Later with (1,10), (3,11) I can get 10 and 33 as the values. Dont worry about what I am trying to calculate here. Just want to know the syntax in doing the above. A simple Python program for this matrix example will help. Many thanks.