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.

4
  • Does this answer your question? How to iterate over rows in a DataFrame in Pandas Commented May 28, 2022 at 11:10
  • @Joooeey how can i apply this here, can you write a Ans please if you already know! Commented May 28, 2022 at 11:21
  • @Joooeey stackoverflow.com/questions/16476924/… --;;;;;;;;;;;;;someone said this optimal way, bu not sure how to apply here Commented May 28, 2022 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. Commented May 28, 2022 at 11:27

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

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!
does this answer help maybe ? Otherwise update your question with a new example and your desired output please
what you said is what i asked for. I just wanted to do with the condition. but its done thanks
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')

1 Comment

AttributeError: Can only use .dt accessor with datetimelike values this is what i get

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.