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.
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.