Reputation: 35
This is my code,i am trying to change the index values of the dataframe into the dates which are present in the dataframe.
import pandas as pd
x = {
'Dates':['24/09/1998', '26/01/1999', '28/08/1999', '11/09/1999'],
'Names': ['A', "B", 'C', 'D'],
'Marks': [5, 8, 5, 9],
'City': ['Rjy', 'Nzmbd', 'Kurnool', 'Srk']}
df = pd.DataFrame(x)
df['Dates']=pd.to_datetime(df['Dates'])
dt = df['Dates']
idx = pd.DatetimeIndex(dt)
df = df.reindex(idx)
print(df)
The output dataframe iam getting is,
Dates Names Marks City
1998-01-01 NaT NaN NaN NaN
1998-01-02 NaT NaN NaN NaN
1998-01-03 NaT NaN NaN NaN
1998-01-04 NaT NaN NaN NaN
1998-01-05 NaT NaN NaN NaN
what should i change in my code so that my data doesn't change to NaN or NaT?
Upvotes: 0
Views: 974
Reputation: 969
You can try this :
In [1]:
## Set the Dataframe
x = {
'Dates':['24/09/1998', '26/01/1999', '28/08/1999', '11/09/1999'],
'Names': ['A', "B", 'C', 'D'],
'Marks': [5, 8, 5, 9],
'City': ['Rjy', 'Nzmbd', 'Kurnool', 'Srk']
}
df = pd.DataFrame(x)
df['Dates']=pd.to_datetime(df['Dates'])
# Change the index
df = df.set_index('Dates')
df
Out [1]:
Names Marks City
Dates
1998-09-24 A 5 Rjy
1999-01-26 B 8 Nzmbd
1999-08-28 C 5 Kurnool
1999-11-09 D 9 Srk
Upvotes: 1
Reputation: 153500
I would do it this way:
x = {
'Dates':['24/09/1998', '26/01/1999', '28/08/1999', '11/09/1999'],
'Names': ['A', "B", 'C', 'D'],
'Marks': [5, 8, 5, 9],
'City': ['Rjy', 'Nzmbd', 'Kurnool', 'Srk']}
df = pd.DataFrame(x)
df = df.set_index('Dates')
df.index = pd.to_datetime(df.index)
print(df)
print('\n')
df.info()
Output:
Names Marks City
Dates
1998-09-24 A 5 Rjy
1999-01-26 B 8 Nzmbd
1999-08-28 C 5 Kurnool
1999-11-09 D 9 Srk
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 4 entries, 1998-09-24 to 1999-11-09
Data columns (total 3 columns):
Names 4 non-null object
Marks 4 non-null int64
City 4 non-null object
dtypes: int64(1), object(2)
memory usage: 128.0+ bytes
Upvotes: 1