0

I currently have a CSV full of information. I am able to import my CSV into numpy (currently using Anaconda/Jupyter, let me know if there is something better I should be using. :)). I am trying to create a new column based on information from 2 other columns in the dataframe. For example, I want to have a code (e.g., A, B, C) for each person based on 2 factors. My first thought is to use if statements, but it doesn't look like I can do that for Dataframes.

Any help or resources on how to get started would be fantastic. I don't have any code at the moment, and am trying to find the best place to start.

This is what I was wanting to try out, but it doesn't look like this would work with that I'm trying to do. Is this still the best place the start? Creating new column using output of if else statement causes error

This is not my code, but the best I could find to start with.

df.loc[df['month'].isin([3,4,5]), 'test'] = 'A'
df.loc[df['month'].isin([6,7,8]), 'test'] = 'B'
df.loc[df['month'].isin([1,2,9,10,11,12]) , 'test'] = 'C'

Would it be possible to move 'column_1' into 'month', and include 'AND' 'column_2', then 'test' = 'A'?

I want to create a new column as A, B, C, etc. based on column_1 and column_2

  • Welcome to stack overflow! Please take a look at [How to create good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide a [mcve] for your question, with sample input and output, so that we can better help you – G. Anderson Jul 23 '19 at 17:00
  • 1
    Ok, I think I've been asking the question in the wrong way. This looks like what I'm trying to accomplish. I will report back after I've worked through this using what I've found. – Pikkster Jul 23 '19 at 17:11
  • it seems as though your solution will be a combination of [this](https://stackoverflow.com/a/34759662/117346410) and [this](https://stackoverflow.com/questions/29757551/pandas-indexing-using-multiple-isin-clauses) – skhan Jul 24 '19 at 16:09

0 Answers0