Reputation: 45
I want to combine the two columns 'Day/Night?' and 'Abbreviated' as strings to get another column that displays the numbers in the form of the 'Abbreviated' value, followed by the 'Day/Night?' value.
The DataFrame I currently have:
Route Date Collection Type Tonnage Day/Night? Abbreviated
0 321-2_2 2014-07-11 2 12.96 2 1107
0 325-1_1 2014-07-14 1 1.85 1 1407
1 321-323_2 2014-07-14 1 4.42 2 1407
1 325-2_1 2014-07-15 3 13.20 1 1507
2 321-2_2 2014-07-15 1 3.25 2 1507
For example, I would have the extra column output as:
11072
14071
14072
15071
15072
I have tried this code:
daily['New'] = daily['Abbreviated']+str(daily['Day/Night?'])
where Daily is the name of the DF.
The output is strange though:
New
11070 2\n0 1\n1 2\n1 1\n2 ...
14070 2\n0 1\n1 2\n1 1\n2 ...
14070 2\n0 1\n1 2\n1 1\n2 ...
15070 2\n0 1\n1 2\n1 1\n2 ...
15070 2\n0 1\n1 2\n1 1\n2 ...
Does anyone have any advice on how I could do this?
thanks.
Upvotes: 3
Views: 470
Reputation: 294478
cols = ['Abbreviated', 'Day/Night?']
df.assign(New=df[cols].astype(str).apply(''.join, 1))
Route Date Collection Type Tonnage Day/Night? Abbreviated New
0 0 321-2_2 2014-07-11 2 12.96 2 1107 11072
1 0 325-1_1 2014-07-14 1 1.85 1 1407 14071
2 1 321-323_2 2014-07-14 1 4.42 2 1407 14072
3 1 325-2_1 2014-07-15 3 13.20 1 1507 15071
4 2 321-2_2 2014-07-15 1 3.25 2 1507 15072
Upvotes: 0
Reputation: 215047
You need to use astype
for type conversion:
df['Abbreviated'].astype(str) + df['Day/Night?'].astype(str)
#0 11072
#0 14071
#1 14072
#1 15071
#2 15072
#dtype: object
Upvotes: 4