0

So, I am working w/data from my research lab and am trying to sort it and move it around etc. And most of the stuff isn't important to my issue and I don't want to go into detail because confidentiality stuff, but I have a big table w/columns and rows and I want to specifically switch the elements of two columns ONLY in one row.

The extremely bad attempt at code I have for it is this (I rewrote the variables to be more vague though so they make sense):

for x in df.columna.values:

*some if statements*

df.loc[df.index([df.loc[df['columna'] == x]]), ['columnb', 'columna']] = df[df.index([df.loc[df['columna'] == x]]), ['columna', 'columnb']].numpy()


I am aware that the code I have is trash (and also the method - w/the for loops and if statements. I know I can abstract it a TON but I just want to actually figure out a way to make it work and them I will clean it up and make it prettier and more efficient. I learned pandas existed on tuesday so I am not an expert), but I think my issue lies in the way I'm getting the row.

One error I was recently getting for a while is the method I was using to get the row was giving me 1 row x 22 columns and I think I needed the name/index of the row instead. Which is why the index function is now there. However, I am now getting the error:

TypeError: 'RangeIndex' object is not callable

And I am just so confused all around. Sorry I've written a ton of text, basically: is there any simpler way to just switch the elements of two columns for one specific row (in terms of x, an element in that row)?

I think my biggest issue is trying to like- get the rows "name" in the format it wants. Although I may have a ton of other problems because honestly I am just really lost.

2 Answers2

0

I think it was probably a syntax problem. I am assuming you are using tensorflow with the numpy() function? Try this it switches the columns based on the code you provided:

for x in df.columna.values:
    # *some if statements*
    df.loc[ 
           (df["columna"] == x), 
           ['columna', 'columnb']
          ] = df.loc[(df["columna"] == x), ['columnb', 'columna']].values.numpy()

I am also a beginner and would recommend you aim to make it pretty from the get go. It will save you a lot of extra time in the long run. Trial and error!

John Ketterer
  • 137
  • 1
  • 1
  • 9
  • Thank you so so much! I don't think I'm using tensorflow though - how would I use that? I'm using jupyter notebook and importing a csv file and using numpy and pandas. I tried the code and I think it makes so much more sense but it did give me an error: AttributeError: 'numpy.ndarray' object has no attribute 'numpy'. I'm guessing this might be connected to me not having tensorflow? – Csentthusiast101 Oct 23 '20 at 01:35
  • And yes! I know I probably should start pretty. I usually do when it's like, in a class and stuff. I just felt a bit like I got thrown off a cliff w/this project because I'd never heard of pandas before haha – Csentthusiast101 Oct 23 '20 at 01:37
  • Awesome, I am not sure why you had the numpy() hanging out at the end in your example code. You do not have to call numpy if you are just going to use the numpy library. Tensorflow is just a library for deep learning, don't worry about that one. You can lose the numpy() function to see if it works. – John Ketterer Oct 23 '20 at 01:53
  • Yeah, I got rid of the numpy. It didn't throw an error but it also didn't work. The code definitely ran because I made sure to check, but for some reason it didn't switch the columns. It did originally give a warning about something to do w/copies and chains. However that warning isn't showing up anymore, but it also still isn't swapping the columns :/, not sure why. – Csentthusiast101 Oct 23 '20 at 02:19
  • You won't see the swapped results in the columns outside of a for loop. This means whatever changes you make to the dataframe are only going to be applicable within the for loop. If you wanted to alter the dataframe by using the for loop (the code inside your for loop) you would have to make a copy as suggested and assign all of the changes made. Take a look at this post. It refers to python2.7 but still applies for latest versions. https://stackoverflow.com/questions/45412865/how-to-change-dataframes-using-a-loop-in-python-pandas – John Ketterer Oct 23 '20 at 04:40
0

You're sooooo close! The error you're getting stems from trying to slice df.index([df.loc[df['columna'] == x]]). The parentheses are unneeded here and this should read as: df.index[df.loc[df['columna'] == x]].

However, here's an example on how to swap values between columns when provided a value (or multiple values) to swap at.

Sample Data

df = pd.DataFrame({
    "A": list("abcdefg"),
    "B": [1,2,3,4,5,6,7]
})

print(df)
   A  B
0  a  1
1  b  2
2  c  3
3  d  4
4  e  5
5  f  6
6  g  7

Let's say we're going to swap the values where A is either "c" or "f". To do this we need to first create a mask that just selects those rows. To accomplish this, we can use .isin. Then to perform our swap, we actually take the same exact approach you had! Including the .to_numpy() is very important, because without it Pandas will actually realign your columns for you and cause the values to not be swapped. Putting it all together:

swap_at = ["c", "f"]
swap_at_mask = df["A"].isin(swap_at) # mask where columns "A" is either equal to "c" or "f"

# Without the `.to_numpy()` at the end, pandas will realign the Dataframe
#  and no values will be swapped
df.loc[swap_at_mask, ["A", "B"]] = df.loc[swap_at_mask, ["B", "A"]].to_numpy()

print(df)
   A  B
0  a  1
1  b  2
2  3  c
3  d  4
4  e  5
5  6  f
6  g  7
Cameron Riddell
  • 10,942
  • 9
  • 19
  • Ah! Thank you so much! I modified that to fit my variables and it worked. And because I don't have to use a for loop it is actually masking over. I really really appreciate your help. And you explanation makes a lot of sense. Thank you so much :)) – Csentthusiast101 Oct 23 '20 at 16:54
  • No problem! Since this worked for you, would you mind selecting this as the correct answer so that users with similar a question can find this reply as well? – Cameron Riddell Oct 23 '20 at 16:57
  • 1
    yes! Sorry, this was my first time asking an actual question on stack overflow so I forgot about that. done :) – Csentthusiast101 Oct 26 '20 at 18:46