0 votes
1 view
in Data Science by (17.6k points)

I would be really grateful for an advice. I had an exercise like it's written bellow:

The first column (index 0) contains year values as four digit numbers in the format YYYY (2016, since all trips in our data set are from 2016). Use assignment to change these values to the YY format (16) in the test_array ndarray.

I used a code to solve it:

test_array[:,0] = test_array[:,0]%100

But I'm sure it has to be more universal and smart way to get the same results with datetime or smth else. But I cant find it. I tried different variations of this code, but I dont get whats wrong:

dt.datetime.strptime(str(test_array[:,0]), "%Y")

test_array[:,0] = dt.datetime.strftime("%y")

Could you help me with this, please?

Thank you

1 Answer

0 votes
by (39.1k points)

Here, we need intermediate datetime value on which operations like strftime can be implemented so that conversion of year from YYYY format to YY can be done in the following manner:

df.iloc[:, 0] = df.iloc[:, 0].apply(lambda x: pd.datetime(x, 1, 1).strftime('%y'))

Here, we need three arguments i.e. year, month and date to obtain datetime values.

We have year values and other values will be 1 by default.

If you want to know more about NumPy visit this Python NumPy Tutorial.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !