0

I once again need your help. I have to change the IDs in my dataset in a rather weird way, but it is, what it is. Now let me show you a line of my dataset:

       number  timestamp  id      x       y
20681       1         12   1  2.035  14.378

I´m locating each line by 'number' with this line of code:

select_number = data_cleaning_sortable.loc[data_cleaning_sortable['number'] == 1]

After that I want to replace the id in the given line with a certain value. The problem is I don´t know the value of the id, since I´m trying to automate this process for the whole dataset. So I really just want to call the label of the column instead of the value. I´ve tried this:

new = select_number.replace(['id'], 4)

But it doesn´t work. I´ve searched a while on stackoverflow, but I couldn´t find anyone with the same problem. Maybe a good soul here can help me out. Thanks in advance.

EDIT: This i what it looks like before editing:

number  timestamp  id      x       y
94540        768     0  10.989  17.986
94541        780     0  10.507  18.147
94542        792     0  10.278  18.491
94543        804     0   9.980  18.812
94544        816     0   9.291  18.996

This is what it should look like after editing:

number  timestamp  id      x       y
94540        768     1  10.989  17.986
94541        780     1  10.507  18.147
94542        792     1  10.278  18.491
94543        804     1   9.980  18.812
94544        816     1   9.291  18.996
ichbindas
  • 31
  • 5
  • 1
    You're trying to update the value in id? Something like `select_number['id'] = 4` or `new = select_number.assign(id=4)` if you need an updated copy? [Add column to DataFrame with constant value](https://stackoverflow.com/q/29517072/15497888) ? – Henry Ecker Sep 01 '21 at 01:33
  • 1
    I think you want to `data_cleaning_sortable.loc[data_cleaning_sortable['number'] == 1, 'id'] = 4` – Trock Sep 01 '21 at 01:36
  • Thank you for the super quick response Henry, your first answer was right :D It´s late at night and i wanted to finish this, so i appreciate your help very much! – ichbindas Sep 01 '21 at 01:38
  • @Trock somehow that didn´t work, when i tried it. It replaced the whole line of the dataset with 4. – ichbindas Sep 01 '21 at 01:40
  • @ichbindas I mean this [conditional-replace-pandas](https://stackoverflow.com/a/44311454/16182150). If you not specify the column of id. it will replace the whole line. `df.loc[df['a']==1, 'b'] = 2` – Trock Sep 01 '21 at 01:49
  • show a raw csv before your change and then show the raw csv after you do the change you want. we can do this without the help of pandas using python standard libraries. – Joshua Sep 01 '21 at 02:55
  • @Joshua i added it to my post, because right now im struggling to save my edited data into a dataframe again. maybe you have a better solution. – ichbindas Sep 01 '21 at 14:44

0 Answers0