1
# Import pandas library
import pandas as pd

# initialize list of lists
data = [['2016-01-02 11:23:04.299000+00:00', 10], ['2016-01-02 11:23:04.299000+00:00', 15], ['2016-01-02 11:23:04.299000+00:00', 14],['2016-01-02 11:23:04.299000+00:00', 10],['2016-01-02 11:23:04.299000+00:00', 10]
       ,['2016-01-02 11:23:04.299000+00:00', 10],['2016-01-02 11:23:04.299000+00:00', 10]]

df = pd.DataFrame(data, columns = ['time', 'sd'])
#df

                                time    sd
0   2016-01-02 11:23:04.299000+00:00    10
1   2016-01-02 11:23:04.299000+00:00    15
2   2016-01-02 11:23:04.299000+00:00    14
3   2016-01-02 11:23:04.299000+00:00    10
4   2016-01-02 11:23:04.299000+00:00    10
5   2016-01-02 11:23:04.299000+00:00    10
6   2016-01-02 11:23:04.299000+00:00    10

I need to do the operation with the time column, which I do as follows.

for i in range(len(df['time'])):
    df.loc[i, 'time'] = pd.Timestamp(df['time'][i]).strftime('%Y-%m-%d %X')

this is my solution.

now the problem is-: is there any other way to make this iteration operation?

because my dataframe Huge and interaction operation is taking time here.

Thanks.

Coder
  • 1,129
  • 10
  • 24
  • Does this answer your question? [How to iterate over rows in a DataFrame in Pandas](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) – Joooeey May 28 '22 at 11:10
  • @Joooeey how can i apply this here, can you write a Ans please if you already know! – Coder May 28 '22 at 11:21
  • @Joooeey https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas#:~:text=Caveats%20section%20above.-,An%20Obvious%20Example,-Let%27s%20demonstrate%20the --;;;;;;;;;;;;;someone said this optimal way, bu not sure how to apply here – Coder May 28 '22 at 11:23
  • sorry I wasn't looking very well. The linked question is not really relevant because this task has a direct implementation in pandas. Stay tuned for my answer. – Joooeey May 28 '22 at 11:27

2 Answers2

2

You can do that directly without manually loop over all rows:

df['time'] = pd.to_datetime(df['time']).dt.strftime('%Y-%m-%d %X')

print(df)
                  time  sd
0  2016-01-02 11:23:04  10
1  2016-01-02 11:23:04  15
2  2016-01-02 11:23:04  14
3  2016-01-02 11:23:04  10
4  2016-01-02 11:23:04  10
5  2016-01-02 11:23:04  10
6  2016-01-02 11:23:04  10
Rabinzel
  • 7,757
  • 3
  • 10
  • 30
  • can I make it into if-else -: so like if format is "%Y-%m-%d %X", then skip, or else... apply this operation. something like this! – Coder May 28 '22 at 12:18
  • does [this answer](https://stackoverflow.com/a/55247202/15521392) help maybe ? Otherwise update your question with a new example and your desired output please – Rabinzel May 28 '22 at 12:22
  • 1
    what you said is what i asked for. I just wanted to do with the condition. but its done thanks – Coder May 28 '22 at 13:16
0

Pandas provides a dedicated method for converting a Series of dates to strings: pd.Series.dt.strftime()

df['time'] = df['time'].dt.strftime('%Y-%m-%d %X')
Joooeey
  • 3,394
  • 1
  • 35
  • 49
  • `AttributeError: Can only use .dt accessor with datetimelike values` this is what i get – Coder May 28 '22 at 12:02