1

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']

  • 2
    It would be helpful if you provide sample code and data, abcarray, for example. – Park Feb 08 '22 at 03:21
  • 1
    numpy does better with multi-dim. pandas is built on it so you certainly have it. check the shape of the numpy array. you can also iterate over it, but beware it may be in y,x form. – Abel Feb 08 '22 at 03:21
  • 2
    Can you share a little more info abut your pd? – IStackoverflowAndIKnowThings Feb 08 '22 at 03:21
  • 1
    I think what you mean is that you need to monitor each row per iteration. This is what your code seems to be doing. You can simply make a for loop through the row and compare elements of the rows from j-th column. Then extract (in this case print) date from the respective row of i-th column. Not sure what you are trying to do, but your code seems like this. – SJa Feb 08 '22 at 03:29
  • Updated the question. I also feel like I could be approaching this wrong with i and j, but do let me know. – Brian Nazareth Feb 08 '22 at 03:41
  • It's still very difficult to tell what your question is. Perhaps you could give us the desired output if you printed the elements instead of calling your irrelevant function `get_image` with them. Also check out [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for info on how to include your sample dataframe in a useful format – AJ Biffl Feb 08 '22 at 04:06

1 Answers1

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
jhso
  • 3,103
  • 1
  • 5
  • 13