Python Forum
How to create 2 dimensional variables in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create 2 dimensional variables in Python?
#1
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.
Reply
#2
Please provide a small, sample of abc.csv, large enough and complete enough to be able to run your snippet.
Reply
#3
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
Reply
#4
(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.

Attached Files

.csv   abc.csv (Size: 94.63 KB / Downloads: 50)
Reply
#5
Not sure I understand the question, but you give it the indexes and it gives you the value
tracker1[25,26] += 1
Reply
#6
(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.
   

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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 957 Nov-14-2023, 08:46 AM
Last Post: perfringo
Question How create programmatically variables ? SpongeB0B 6 1,324 Aug-19-2023, 05:10 AM
Last Post: SpongeB0B
  How to quantize a 4 dimensional array? PythonNPC 2 1,626 Apr-23-2022, 04:34 PM
Last Post: Gribouillis
  Create array of values from 2 variables paulo79 1 1,095 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  Slicing a 2 dimensional array Scott 2 1,664 Jan-12-2022, 07:18 AM
Last Post: paul18fr
  Problem using two-dimensional interpolation. Result looks bad player1682 4 2,520 Oct-12-2021, 09:27 AM
Last Post: player1682
  Index data must be 1-dimensional : Classifier with sklearn Salma 0 4,325 Apr-01-2021, 03:22 PM
Last Post: Salma
  2 Dimensional Arrays Prithak 4 2,598 Mar-21-2021, 09:35 PM
Last Post: deanhystad
  Create new variable dependent on two existing variables JoeOpdenaker 6 3,027 Oct-25-2020, 02:15 PM
Last Post: jefsummers
  Create, assign and print variables in loop steven_tr 10 4,358 May-28-2020, 04:26 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020