#๐Ÿ”’ Need Help with Pandas and Excel

10 messages ยท Page 1 of 1 (latest)

burnt hare
#

Hi, so I'm new to python and was trying out simple stuff sorting/handling excel sheets with Python. I currently have a sample database which has employee_position as a column. I want to increase the salaries of all the Managers by 10% and add it to a new column using Pandas but can't seem to find the right answer to it. Please advise. Thanks!

sterile magnetBOT
#

@burnt hare

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.

dry dirge
#

a sample database
You mean an excel sheet, or an actual database?

What have you done so far? Did you get the sheet into a pandas dataframe?

burnt hare
#

Hi! Thanks for the quick reply. It is an xlsx sheet. I've tried sorting values by columns and cleaning data so far. I got the sheet into a dataframe for the task at hand but can't seem to figure out the syntax for increasing employees salary by specific values in the employee_position column.

dry dirge
#

Well, you have a condition (employee_position is a certain value) and want to either keep the salary the same or increase it depending on it. That's what where does. So you could do something like

df["new_salary"] = df.salary.where(df.employee_position != "Manager", df.salary * 1.1)
#

alternatively, copy the column and then modify a slice:

df["new_salary"] = df.salary
df.loc[df.employee_position == "Manager", "new_salary"] *= 1.1
#

(whoops, messed up the conditional in the first one, fixed now)

burnt hare
#

Haha, yeah the edited version worked! Thanks a ton! Still figuring stuff out so I maybe around with some more questions in the future pithink

sterile magnetBOT
#
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.