#๐Ÿ”’ Row equivalent of .columns in pandas?

24 messages ยท Page 1 of 1 (latest)

stray spade
#

I would have assumed its just .rows but it isn't

pure oysterBOT
#

@stray spade

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

warm cypress
#

if you are trying to iterate over the rows like you would over a python list, don't (there are methods for that but it is a LOT slower than using pandas methods)

stray spade
#

I'm not trying to iterate over rows, I was trying to save the rows to a different variable but I actually do not think thats what I need to do lol.. I'm working on a data classification assignment so I don't think the rowname will help with anything

#

Is there a way to return column name?

sullen yarrow
#

what do you mean by "save the rows to a different variable"? what type would that object be?

stray spade
#

Yeah the logic there wasn't really sound, I don't even know if that thought is possible/makes sense

warm cypress
#

what are you even trying to do in first place though?

#

not in code, not your attempted solution, just the problem you are trying to solve

stray spade
#

I'm sorry I know its confusing, part of me doesn't exactly know what to do. I was attempting to breakup the dataframe into two different parts I guess

sullen yarrow
#

you can do something like

df_1 = df.loc[['a', 'b', 'c']]
df_2 = df.loc[['d', 'e', 'f']]

where those letters are indices for rows

but only if there's a reason why having some rows in different dataframes is better than just having one.

stray spade
#

Okay, that makes sense. I think I actually need to split it by columns, not rows. Is there a way to index to save just the last column?

#
df[-1]```
sullen yarrow
#

you would do df.iloc[:, -1] for that

stray spade
#

Ok, how would I save everything but the last column then?

sullen yarrow
#

df.iloc[:, :-1]

#

it's like list slicing.

stray spade
#

Makes sense. I wasn't familiar with the syntax of using ':'

#

I think that solved it. thank you

pure oysterBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ Row equivalent of .columns in pandas?