# Replace data from a data frame

**URL:** <https://discuss.python.org/t/replace-data-from-a-data-frame/8534>\
**Category:** Python Help\
**Tags:** help\
**Created:** [May 4, 2021, 12:36am UTC](https://discuss.python.org/t/replace-data-from-a-data-frame/8534 "2021-05-04T00:36:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmoraga](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@fmoraga](https://discuss.python.org/u/fmoraga)\
**Post date:** [May 4, 2021, 12:36am UTC](https://discuss.python.org/t/replace-data-from-a-data-frame/8534/1 "2021-05-04T00:36:38Z")

</div>

Hello to the whole community:  
I have a question, and I want to ask for help, please.  
I have a data frame, with data between 0 and 1 (see the image). The data can be for example 0 or also 0.45, etc.

 ![Dataframe](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/7/798048ec263fba19e586c85a67490fe8804f565f.png)

I need to rewrite that table, but by categories or sections, that is, for example, all the numbers that are between 0 and 0.2, replace them with 0.1. If the numbers are between 0.5 and 0.78556, it should be replaced by 0.6 (this is just an example).

How could I do it? I was thinking of something like:

for i in dataframe1[0:23]:

for j in dataframe1.index:

```
if 0<dataframe1.getloc[j,i]<10:

  dataframe2=dataframe1.replace(dataframe1.loc[j,i],8)

```

dataframe2

But I have not had good results.

I would appreciate any help, please.

---

<div class="post-metadata">

**Author:** ![edusanketdk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/edusanketdk/32/3986_2.png) [@edusanketdk](https://discuss.python.org/u/edusanketdk)\
**Post date:** [May 6, 2021, 12:04pm UTC](https://discuss.python.org/t/replace-data-from-a-data-frame/8534/2 "2021-05-06T12:04:21Z")

</div>

I have some questions that you should consider answering to help me get your requirement known well:

- What is the logic behind rounding off the numbers?
- Do you mean to just rewrite the rounded number at the same place or do something else with the section thing?
- Can you just show an example of expected dataframe?

---

<div class="post-metadata">

**Author:** ![edusanketdk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/edusanketdk/32/3986_2.png) [@edusanketdk](https://discuss.python.org/u/edusanketdk)\
**Post date:** [May 6, 2021, 12:09pm UTC](https://discuss.python.org/t/replace-data-from-a-data-frame/8534/3 "2021-05-06T12:09:37Z")

</div>

If you just want to replace the rounded number at the same place, you can directly process on the dataframe.

For example if you want to round the numbers in a 0.2 difference range like-  
**Input:**  
`....A....B `  
` 0 0.224 0.123`  
` 1 0.323 0.555`  
` 2 0.923 0.099`

**Output:**  
`...A....B `  
` 0 0.2 0.0`  
` 1 0.2 0.4`  
` 2 0.8 0.0`

You can do it directly like - `df = (df//0.2)*0.2`.
