Reputation: 617
I've got a pandas Serie in a DF, like this one :
DF=pd.DataFrame([1,2,58,99,123,256],columns=["ID"])
ID
0 1
1 2
2 58
3 99
4 123
5 256
I'm trying to modify "ID" column, in order to have something like this (always three characters, so one or two zeros for numbers with length<3) :
ID
0 "001"
1 "002"
2 "058"
3 "099"
4 "123"
5 "256"
I tried to achieve this with a list comprenhension, astype(str), replace() and str.len(), like this :
DF["ID"]=[x.replace(x, i*"0"+x) for x,i in (DF["ID"].astype(str),int(DF[DF["ID"]].astype(str).len())-3)]
But then I had this Error
KeyError: "None of [Int64Index([1, 2, 58, 99, 123, 256], dtype='int64')] are in the [columns]"
How could I fix it ?
Upvotes: 1
Views: 80
Reputation: 78780
In my opinion the most idiomatic way is:
>>> DF['ID'] = DF['ID'].astype(str).str.rjust(3, '0')
>>> DF
ID
0 001
1 002
2 058
3 099
4 123
5 256
Upvotes: 0
Reputation: 8219
you an use f-strings with formatting
DF['ID'] = DF['ID'].apply(lambda v: f'{v:03d}')
output (these are string btw)
ID
0 001
1 002
2 058
3 099
4 123
5 256
Upvotes: 0
Reputation: 78
The error you are getting is because you are trying to pass the values in the your 'ID' series as if it were a list of column labels. There is a much simpler way of doing this using pandas apply and the str.rjust() method setting the width to 3 to pad the strings to 3 characters:
df = pd.DataFrame(dict(ID=[1,2,58,99,123,256]))
df['ID'] = df['ID'].apply(lambda x: str(x).rjust(3,'0'))
Outputs:
ID
0 "001"
1 "002"
2 "058"
3 "099"
4 "123"
5 "256"
Upvotes: 1