Add a column to Dataframe

Source: Internet
Author: User
Nathan and I have been working on the Titanic kaggle problem using the Pandas data Analysis library and one thing we wante D To do is add a column to a dataframe indicating if someone survived.

We had the following (simplified) dataframe containing some information about customers on board the Titanic:

def addrow (DF, Row): Return
     df.append (PD. Dataframe (Row), ignore_index=true)
customers = PD. Dataframe (columns=[' Passengerid ', ' pclass ', ' Name ', ' Sex ', ' Fare '])
customers = AddRow (Customers, [Dict ( passengerid=892, pclass=3, name= "Kelly, Mr James", sex= "male", fare=7.8292)]
customers = addrow (Customers, [Dict ( passengerid=893, pclass=3, name= "Wilkes, Mrs James (Ellen Needs)", sex= "female", fare=7)]
>>> Customers
     Fare                              Name  passengerid  pclass     Sex
0  7.8292                  Kelly, Mr. James          892       3    male
1  7.0000  Wilkes, Mrs James (Ellen Needs)          893       3  Female

We wanted to add a ' survived ' and ' to ' of doing a lookup in the survival_table below to work out the appropriate Val Ue:

survival_table = PD. Dataframe (columns=[' Sex ', ' pclass ', ' pricedist ', ' survived '])   survival_table = AddRow (survival_table, [Dict (  Pclass=1, sex= "female", Pricedist = 0, survived = 0)]) survival_table = AddRow (survival_table, [Dict (Pclass=1, sex= "male", Pricedist = 0, survived = 0)] survival_table = AddRow (survival_table, [Dict (Pclass=1, sex= "female", pricedist = 1, Survi ved = 0)] survival_table = AddRow (survival_table, [Dict (Pclass=1, sex= "male", Pricedist = 1, survived = 0)]) survival_tab Le = AddRow (survival_table, [Dict (Pclass=1, sex= "female", Pricedist = 2, survived = 1)]) survival_table = AddRow (Survival_ Table, [Dict (Pclass=1, sex= "male", Pricedist = 2, survived = 0)]) survival_table = AddRow (survival_table, [Dict (Pclass=1,  sex= "female", Pricedist = 3, survived = 1)]) survival_table = AddRow (survival_table, [Dict (Pclass=1, sex= "male", pricedist  = 3, survived = 0)]   survival_table = AddRow (survival_table, [Dict (pclass=2, sex= "female", Pricedist = 0, survived = 0)]) survival_table = AddRow (survival_table, [Dict (pclass=2, sex= "male", Pricedist = 0, survived = 0)]) survival_table = AddRow (su Rvival_table, [Dict (pclass=2, sex= "female", Pricedist = 1, survived = 1)]) survival_table = AddRow (survival_table, [Dict (P class=2, sex= "male", Pricedist = 1, survived = 0)]) survival_table = AddRow (survival_table, [Dict (pclass=2, sex= "female",  Pricedist = 2, survived = 1)] survival_table = AddRow (survival_table, [Dict (pclass=2, sex= "male", Pricedist = 2, survived = 0)] survival_table = AddRow (survival_table, [Dict (pclass=2, sex= "female", Pricedist = 3, survived = 1)]) survival_tabl E = AddRow (survival_table, [Dict (pclass=2, sex= "male", Pricedist = 3, survived = 0)])   survival_table = AddRow (Survi Val_table, [Dict (Pclass=3, sex= "female", Pricedist = 0, survived = 1)]) survival_table = AddRow (survival_table, [Dict (PCLA ss=3, sex= "male", Pricedist = 0, survived = 0)]) survival_table = AddRow (survival_table, [Dict (Pclass=3, sex= "female", Pri Cedist = 1, survived = 1)] survival_table = AddRow (survival_table, [Dict (pclass=3, sex= "male", Pricedist = 1, survived = 0)]) survival_table = AD  Drow (survival_table, [Dict (Pclass=3, sex= "female", Pricedist = 2, survived = 0)]) survival_table = AddRow (survival_table,
[Dict (pclass=3, sex= "male", Pricedist = 2, survived = 0)]) survival_table = AddRow (survival_table, [Dict (Pclass=3, sex= "female", Pricedist = 3, survived = 0)]) survival_table = addr
    ow (Survival_table, [Dict (pclass=3, sex= "male", Pricedist = 3, survived = 0)])   >>> survival_table          Pclass Pricedist Sex survived 0 1 0 female 0 1 1 0 male 0 2        1 1 Female 0 3 1 1 male 0 4 1 2 female 1 5        1 2 Male 0 6 1 3 female 1 7 1 3 Male 0 8     2 0 Female 0 9 2 0 male 0 10 2      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.