I want to change the Month and Day column in the Python pandas dataframe from integer to strings with leading zeros.
What I want is here: the input is here as a pandas dataframe:
Year Month Day
2018 1 1
2018 1 12
2018 1 18
2018 2 4
2018 2 1
2018 2 2
2018 2 12
2018 3 30
I want to make them like this:
Year Month Day
2018 01 01
2018 01 12
2018 01 18
2018 02 04
2018 02 01
2018 02 02
2018 02 12
2018 03 30
My method is pretty dumb, and it is very slow.
def Import():
df = pd.read_csv('Transaction_data.csv',index_col=0)
n = len(df)
for i in range(n):
temp = df['Year'].loc[i]
df['Year'].loc[i] = str(temp)
for i in range(n):
temp = df['Month'].loc[i]
if temp<10:
df['Month'].loc[i] = '0'+str(temp)
else:
df['Month'].loc[i] = str(temp)
for i in range(n):
temp = df['Day'].loc[i]
if temp<10:
df['Day'].loc[i] = '0'+str(temp)
else:
df['Day'].loc[i] = str(temp)
return df
Also
pd.to_datetime(df['Month'],format='%d')
won't help since to_datetime only has month as integers [1,12]
I want to change the Month and Day column in the Python pandas dataframe from integer to strings with leading zeros.
Use series.str.zfill()
:
df[['Month','Day']]=df[['Month','Day']].astype(str).apply(lambda x: x.str.zfill(2))
print(df)
Year Month Day
0 2018 01 01
1 2018 01 12
2 2018 01 18
3 2018 02 04
4 2018 02 01
5 2018 02 02
6 2018 02 12
7 2018 03 30
You mentioned you wish to have a string value so you can use a simple lambda. On the Day example you should have:
df['Day'].apply(lambda x: "0"+str(x) if x<10 else x)
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.