Ayush Agarwal
Ayush Agarwal

Reputation: 57

How to add a new series by combining two series from a dataframe?

I have a dataframe district_df which contains series Borough such as Bronx, Brooklyn, Manhatten etc. and another series Borough number such as 2, 4, 8 etc. i want to create another series Board in that dataframe combining Borough and borough number such as 02 Bronx, 04 Brooklyn, 08 manhatten etc,

Borough CD Number
Bronx    2
Brooklyn 4 
Manhatten 8  

into

Borough  CD Number    Board
Bronx     2          02 Bronx
Brooklyn  4          04 Brooklyn
Manhatten 8          08 Brooklyn

Upvotes: 1

Views: 41

Answers (1)

yatu
yatu

Reputation: 88305

You could use str.cat for the concatenation and fill the CD Number with the required amount of zeros using str.zfill:

df['Board'] = df['CD Number'].astype(str).str.zfill(2).str.cat(df.Borough, sep=' ')    

      Borough     CD Number    Board
0      Bronx          2        02 Bronx
1   Brooklyn          4      04 Brooklyn
2  Manhatten          8      08 Manhatten

Upvotes: 2

Related Questions