I am getting syntax error with the .merge() Pandas function.
What am I doing wrong and how can I fix it?
Below is a snippet of my code;
df2 = df2.merge(df1[list('col1','col2'], on ='ABC')
str.zfill(x) will put x zeroes before any string
If you'd rather use string formatting, f'{str:0x}' also works.
P.S. Don't name your strings str as it overloads the str class
Using str.zfill(width):
The method zfill() pads string on the left with zeros to fill width.
width − This is final width of the string. This is the width which we would get after filling zeros.
id = [6, 77, 888, 9999]
print([str(x).zfill(4) for x in id])
OUTPUT:
['0006', '0077', '0888', '9999']
EDIT:
To convert the column values in a column using list-comprehension:
import pandas as pd
df = pd.DataFrame({'un-padded':[6, 77, 888, 9999]})
df['padded'] = [str(x).zfill(4) for x in df['un-padded']]
print(df)
OUTPUT:
un-padded padded
0 6 0006
1 77 0077
2 888 0888
3 9999 9999