How to iterate mutli-dimensional arrays in Python?

91
April 09, 2022, at 3:00 PM

Community. I'm working with a Pandas Dataframe and I've reached a point where I need to sample two columns from the DataFrame. I want to do this by converting the sample to a multi-dimensional array but I do not know how to go from there. This is what I have done so far:

for i,j in abcarray:
    if 'positive' in j:
        print(abcarray[i])
        get_image("img1")
    elif 'negative' in j:
        print(abcarray[i])
        get_image("img2")
    elif 'neutral' in j:
        print(abcarray[i])
        get_image("img3")

This is only supposed to print the ith element while the jth element is whatever I've specified. Now usually in Java, I could have a counter variable that iterates through the rows, but how do you do this in Python? I know that I could use "range(len(abcarray))" but it still doesn't help. I'm very new to Python but I have to use Python for this for obvious reasons.

Edit: Adding a bit more context here after the first few replies. So get_image is a function that checks the value and returns the appropriate image. That part of the code works absolutely fine so I did not include that.

The Dataframe is from a CSV but the sample looks like this

["Review Review Review Text Text Text Text" 'positive'] ["Review Review Review Text Text Text Text" 'negative'] ["Review Review Review Text Text Text Text" 'neutral'] ["Review Review Review Text Text Text Text" 'positive'] ["Review Review Review Text Text Text Text" 'positive']

Answer 1

I've taken a stab at what I think you're trying to do. The key is that you've got a nested list which is something like:

myList = [['review is like this', 'positive'],
          ['i dont like', 'negative'],
          ['meh','neutral']]

Doing a for loop the way you've done is not how it's done in python. For loops iterate over objects (such as lists and arrays) and return the element that it's currently up to (which can be any data type). If you want to return the index that it's up to you can either use the range(len(list)) method which you've mentioned , or use

for ix, i in enumerate(myList):
     ...

which returns the index ix and the element of the list i.

In regards to your problem, I assume you just want something as simple as:

def get_image(string):
    #dummy function which just shows which image is being called
    print(string)
for i in myList:
    print(i[0]) #this is the same no matter the if statement
    if 'positive' in i:
        get_image("img1")
    elif 'negative' in i:
        get_image("img2")
    elif 'neutral' in i:
        get_image("img3")

Which outputs:

review is like this
img1
i dont like
img2
meh
img3
Rent Charter Buses Company
READ ALSO
How do you connect to an existing database table in peewee?

How do you connect to an existing database table in peewee?

Much of the tutorials I've read show how to connect to the database and create a new tableMaybe I am wrong or misunderstanding , Ive reading the documents here: peewee offical

106
Run hive queries on empty dataframe using pyspark

Run hive queries on empty dataframe using pyspark

I want to test my complex hive queries beforehand by executing on empty dataframes using pyspark or pandasHow can I do this

98
Trying to create filters for images, getting integer argument expected errors, screenshots and code included

Trying to create filters for images, getting integer argument expected errors, screenshots and code included

so I am taking a Python course on Codio and the current topic is image manipulation, with the assignment being to create certain filters for images

186
How to change between iframes in Selenium and Python

How to change between iframes in Selenium and Python

I was trying to understand how to change between iframes in selenium webdriver, but I could not figure it outIn the image ahead, is the iframe that I want to switch to:

118