0

I'm new to pandas so please go easy on me,

I'm changing the values in a specific column of my data frame by iterating through the column and changing it. the code is shown below.

row = 0
for values in nonNumericColumns['timestamp in UTC']:
    values = values[:9]
    nonNumericColumns.at[row, 'timestamp in UTC'] = values
    row+=1 

the values in the column before the change is :

0     2022-02-19T06:33:13.582610
1     2022-02-19T06:33:27.737695
2     2022-02-19T06:33:35.754507
3     2022-02-19T06:33:45.681506
4     2022-03-03T08:41:44.297195
5     2022-03-03T08:43:04.868416
6     2022-03-03T08:50:45.769626
7     2022-03-03T08:52:10.764062
8     2022-03-03T09:24:23.985866
9     2022-03-03T09:25:20.357228
10    2022-03-03T09:30:22.536442
11    2022-03-03T09:51:31.012427

now the values after the slicing is done are:

0     2022-02-1
1     2022-02-1
2     2022-02-1
3     2022-02-1
4     2022-03-0
5     2022-03-0
6     2022-03-0
7     2022-03-0
8     2022-03-0
9     2022-03-0
10    2022-03-0
11    2022-03-0

But the problem is that this method of iterating is making the program slow. can someone tell me how to make it efficient?

Any help would be appreciated.

sparsh
  • 135
  • 1
  • 1
  • 11
  • 1
    use the vector slicing: `nonNumericColumns['timestamp in UTC'] = nonNumericColumns['timestamp in UTC'].str[:9]` – mozway Mar 03 '22 at 09:57
  • In complement of @mozway's comment, take a look at [Working with text data](https://pandas.pydata.org/pandas-docs/stable/user_guide/text.html#working-with-text-data) – Corralien Mar 03 '22 at 10:02

0 Answers0