0
AuthorID    CityArrival CountryArrival  Departure   CountryDeparture    DateDeparture   DateArrival
1922    Paris   France  New York    UnitedState 2008-03-10  2001-02-02
1002    LosAngeles  UnitedState California  UnitedState 2008-03-10  2008-12-01
1901    Paris   France  Lagos   Nigeria 2001-03-05  2001-02-02
1922    Paris   France  NewYork UnitedState 2011-02-03  2008-12-01
1002    Paris   France  California  UnitedState 2003-03-04  2002-03-04
1099    Paris   France  Beijing China   2011-02-03  2009-02-04
1901    LosAngeles  UnitedState Paris   France  2001-03-05  2001-02-02

I want to concatenate CityArrival and CountryArrival.

1
  • You mean to combine them into one column? Commented Dec 1, 2015 at 18:33

3 Answers 3

2
import pandas as pd
df = pd.read_csv(path)
df['Arrival'] = df.CityArrival + ' ' + df.CountryArrival
Sign up to request clarification or add additional context in comments.

3 Comments

AuthorID CityArrival CountryArrival Departure CountryDeparture DateDeparture DateArrival 1922 Paris France New York UnitedState 2008-03-10 2001-02-02 1002 LosAngeles UnitedState California UnitedState 2008-03-10 2008-12-01 1901 Paris France Lagos Nigeria 2001-03-05 2001-02-02 1922 Paris France NewYork UnitedState 2011-02-03 2008-12-01 1002 Paris France California UnitedState 2003-03-04 2002-03-04 1099 Paris France Beijing China 2011-02-03 2009-02-04 1901 LosAngeles UnitedState Paris France 2001-03-05 2001-02-02
import pandas as pd import datetime import pandas.io.data from pandas_datareader import data, wb df = pd.read_csv('testfile.csv') df['Arrival'] = df.CityArrival + ' ' + df.CountryArrival print df
And what's the problem?
1
import csv    
with open(path) as f:
    reader = csv.DictReader(path)
    for line in reader:
        arrival = "{}, {}".format(line['CityArrival'], line['CountryArrival'])

2 Comments

Thanks for your quick response, I have another question. From the "Arrival". I want to create dictionary using each value under the arrival (e.g. ParisFrance = { 2001-02-01: [1922]}) so that date will be the key while any of the authorid that fall within the date will be added to the list.
Use collections.defaultdict for this. You can initialize a dictionary like "travel_dict = defaultdict(lambda: defaultdict(list))", then add values like "travel_dict["ParisFrance"]["2001-02-01"].append(1922)"
1

Try this:-

import pandas as pd
df = pd.read_csv(path)
df['Arrival'] = df['CityArrival'].map(str) + ' ' + df['CountryArrival'].map(str)

# update file
df.to_csv('final.csv', encoding='utf-8-sig', index=False)

1 Comment

Ty for .map(str) !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.