Home > Software engineering >  iteration of the rows in a optimize way in python
iteration of the rows in a optimize way in python

Time:05-28

# 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.

CodePudding user response:

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')

CodePudding user response:

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
  • Related