Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

dataframe - How do I change a date to numerical order in pandas?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seens that you could use pandas.factorize to solve your problem.

pandas.factorize

pandas.factorize(values, sort=False, na_sentinel=- 1, size_hint=None)

This method is useful for obtaining a numeric representation of an array when all that matters is identifying distinct values. factorize is available as both a top-level function pandas.factorize(), and as a method Series.factorize() and Index.factorize().

You can try something like this.

import pandas as pd

dates = pd.Series(['1/5/2020', '1/5/2020', '2/5/2020', '3/5/2020', '3/5/2020', '8/5/2020', '8/5/2020', '8/5/2020'])
dates_with_data = pd.DataFrame({"dates": dates, "data": dates})
(factorized_dates, categorical_dates) = dates_with_data["dates"].factorize()
dates_with_data['dates'] = factorized_dates
dates_with_data.head()

I hope this have been useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...