-2

I need to add a few characters to certain cells in the dataframe. When I print xx in the loop, the values are as desired. But when I print the entire dataframe, the values were unchanged:

for row in df['Time from AtoC']:
    if ':' not in row:
        xx = "00:" + row
#         print(xx)
        df.at['Time from AtoC'] = xx

    else:
        df.at['Time from AtoC'] = row

The results I get are

179 rows of data when I only had 100, and some of the values remained unchanged (without 00: in front).

chris
  • 3
  • 2
  • 1
    post sample data and expected output – Sociopath Mar 31 '19 at 03:50
  • 1
    And avoid looping over dataframe or column. There are alternatives available in `pandas` – Sociopath Mar 31 '19 at 03:52
  • check with `df['Time from AtoC']=np.where(df['Time from AtoC'].str.contains(':'),"00:"+df['Time from AtoC'],df['Time from AtoC'])` also check [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – anky Mar 31 '19 at 04:44

1 Answers1

0

It looks like you are trying to add 00 to the front of your times (that are strings) if they are not there? You can put your decision logic in a function and then apply it to the column all at once. (As a bonus, you could do this all in one line with an anonymous function (lambda).

def add_00(time):
   if ':' not in time:
       return "00:" + time
   return time

df['Time from AtoC'] = df['Time from AtoC'].apply(add_00)
Nick Tallant
  • 315
  • 3
  • 6