Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Formatting using many pandas columns to create a new one

I would like to create a new columns in a pandas DataFrame just like I would do using a python f-Strings or format function. Here is an example:

df = pd.DataFrame({"str": ["a", "b", "c", "d", "e"],
                   "int": [1, 2, 3, 4, 5]})

print(df)

  str  int
0   a    1
1   b    2
2   c    3
3   d    4
4   e    5

I would like to obtain:

  str  int concat
0   a    1   a-01
1   b    2   b-02
2   c    3   c-03
3   d    4   d-04
4   e    5   e-05

So something like:

concat = f"{str}-{int:02d}"

but directly with elements of pandas columns. I imagine the solution is using pandas map, apply, agg but nothing successful.

Many thanks for your help.

like image 903
Samovian Avatar asked Mar 31 '20 07:03

Samovian


1 Answers

Use lsit comprehension with f-strings:

df['concat'] = [f"{a}-{b:02d}" for a, b in zip(df['str'], df['int'])]

Or is possible use apply:

df['concat'] = df.apply(lambda x: f"{x['str']}-{x['int']:02d}", axis=1)

Or solution from comments with Series.str.zfill:

df["concat"] = df["str"] + "-" + df["int"].astype(str).str.zfill(2)

print (df)
  str  int concat
0   a    1   a-01
1   b    2   b-02
2   c    3   c-03
3   d    4   d-04
4   e    5   e-05
like image 121
jezrael Avatar answered Nov 13 '22 14:11

jezrael