Tenchu
Tenchu

Reputation: 13

PANDAS - How do you concatenate date and text(string) to a new column?

i'm fairly new to pandas, i downloaded an excel file which contains public law enforcement data, i wanted to concatenate a date (M/D/Y) column with another column that only has texts and add them in a new column. the columns that i want to concatenate is called 'Primary Offense Description' & the other column is called 'Occurred Date'.

Feel free to ask for the excel file & i shall upload it.

Upvotes: 1

Views: 3954

Answers (1)

default_settings
default_settings

Reputation: 490

here is an example dataset,

df1 = pd.DataFrame({
"Date" : ['2013-11-24', '2013-11-24', '2013-11-24', '2013-11-24'],
"Fruit" : ['Banana', 'Orange', 'Apple', 'Celery'], 
"Num" : [22.1, 8.6, 7.6, 10.2],
"Color" : ['Yellow', 'Orange', 'Green', 'Green']
})

the following line of code will create for you new column named "Combination" in the dataset which combine column Date & Color : df1["Combination"] = df1["Date"] + df1["Color"]

Upvotes: 1

Related Questions