Guitarman045
Guitarman045

Reputation: 37

How to convert Days in dataframe column to an integer in pandas?

I have the following column in my dataframe (it has many more rows but they are all in this format):

1st Year Score
365 days 00:00:00
163 days 00:00:00
365 days 00:00:00
143 days 00:00:00
365 days 00:00:00
365 days 00:00:00
52 days 00:00:00

And I want to produce this:

1st Year Score
365 
163 
365 
143 
365 
365 
52 

I have tried the following:

data['1st Year Score'] = data['1st Year Score'].dt.days()

AttributeError: Can only use .dt accessor with datetimelike values

I also tried:

data['1st Year Score'] = pd.to_datetime(data['1st Year Score'],errors='coerce')

which deleted everything in my 1st year column. Let me know how to fix this, thank you.

Upvotes: 1

Views: 1716

Answers (2)

Anurag Dabas
Anurag Dabas

Reputation: 24314

Try via pd.to_timedelta():

df['1st Year Score']=pd.to_timedelta(df['1st Year Score']).dt.days

output of df['1st Year Score']:

0    365
1    163
2    365
3    143
4    365
5    365
6     52
Name: 1st Year Score, dtype: int64

Upvotes: 4

not_speshal
not_speshal

Reputation: 23146

Try:

>>> df["1st Year Score"].str.split(" days").str[0].astype(int)
0    365
1    163
2    365
3    143
4    365
5    365
6     52
Name: 1st Year Score, dtype: int32

Upvotes: 1

Related Questions