0
$\begingroup$

I need a separate date column(in this format2018-10-08) in this table. please help.

   id   id2     post    id  level   created_at
0   150     126293  BA     237602   0   2018-10-08 15:37:06.404741
1   150     126293  BA     217350   0   2018-09-26 11:27:20.610651
2   4655    169068  BA     172174   1   2018-08-07 15:54:15.961607
3   14058   201462  BA      231703  1   2018-10-04 18:26:06.915134
4   16576   241053  agent   416451  2   2019-02-06 16:59:41.550580
5   16576   241053  agent   423247  2   2019-02-10 18:10:12.987203
6   16700   102938  BA     476606   0   2019-03-07 23:23:36.902590
7   50374   131808  TSM     203392  0   2018-09-13 21:54:45.874108
8   50374   131808  TSM     103873  0   2018-03-23 17:30:20.647447
$\endgroup$
  • 1
    $\begingroup$ You may read documentation before asking. date handling, column selection $\endgroup$ – Manu H Apr 2 '19 at 9:32
  • $\begingroup$ Did you mean to get only the date (2018-10-08) and remove the time? $\endgroup$ – ipramusinto Apr 2 '19 at 11:02
1
$\begingroup$
df['date'] = pd.to_datetime(df['created_at']).dt.date

to_datetime: Convert argument to datetime. For example, if your column of "created_at" is a string column, it converts it to a datetime column

dt: Access object for datetime like properties of the Series values.

| improve this answer | |
$\endgroup$
  • $\begingroup$ This is probably the best (code) answer so far, but you should probably include some words e.g explaining why first using pd.to_datetime is necessary and what .dt. is. Links to documentation are also a good idea :) $\endgroup$ – n1k31t4 May 30 at 7:54
  • $\begingroup$ Now it is a complete answer! :) $\endgroup$ – n1k31t4 May 30 at 17:42
0
$\begingroup$

You can achieve this by using the following code:

date = df['created_at']
df.drop('created_at', inlace = True)
| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ Please consider adding some explanation of what your code does. Ultimately, the output of it. $\endgroup$ – Tasos Apr 2 '19 at 9:34
0
$\begingroup$
  df['date'] = df['created_at'].str.extract('(....-..-..)', expand=True)
| improve this answer | |
$\endgroup$
0
$\begingroup$

If the created_at column is of the datetime type, you can use the .dt method to only get the date as follows:

df["date_column"] = df["created_at"].dt.date

This will return the following column:

date_column
2018-10-08
2018-09-26
2018-08-07
2018-10-04
2019-02-06
2019-02-10
2019-03-07
2018-09-13
2018-03-23
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.