If there is a column called date
in a pandas.DataFrame
, For which the values are:
'2018-02-01',
'2018-02-02',
...
How do I change all the values to integers? For example:
'20180201',
'20180202',
...
You can use .str.replace()
like:
df['newdate'] = df['date'].str.replace('-', '')
or if not using a regex, faster as a list comprehension like:
df['newdate'] = [x.replace('-', '') for x in df['date']]
df = pd.DataFrame(['2018-02-01', '2018-02-02'], columns=['date'])
print(df)
df['newdate'] = df['date'].str.replace('-', '')
print(df)
df['newdate2'] = [x.replace('-', '') for x in df['date']]
print(df)
date
0 2018-02-01
1 2018-02-02
date newdate
0 2018-02-01 20180201
1 2018-02-02 20180202
date newdate newdate2
0 2018-02-01 20180201 20180201
1 2018-02-02 20180202 20180202
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.