2

Sorry if this is a really dumb question. I'm a total noob at pandas and can't even figure out what key words to use to search for a solution for the problem I have.

Basically, I have a numeric data frame,

numeric_df = pd.DataFrame({"colA": [1.23, 2.34, 3.45],
                    "colB":[1.00, 2.00, 3.00]})

Now I create a second df that duplicates the value of numeric_df

formatted_df = numeric_df

Then I format the two columns in "formatted_df" according my needs, I'm doing it this way because I want to keep the values in numeric_df as numbers, so I can operate on them later.

formatted_df["colA"] = formatted_df["colA"].map("${:}".format)
formatted_df["colB"] = formatted_df["colB"].map("{:}Years".format)

But now, if I view numeric_df, its columns are already formatted and converged into a string. What is causing the problem? Why does my map method modify the original data frame?

Thank you in advance for any help you can give.

cs95
  • 379,657
  • 97
  • 704
  • 746
Clara
  • 106
  • 5

1 Answers1

1

Using formatted_df = numeric_df mean the variables share the same memory footprint. Referencing the same object. To manipulate one independently you need a seperate object. For this you can clone an object or Pandas offers copy()

formatted_df = numeric_df.copy()

why should I make a copy of a data frame in pandas

Lex
  • 4,749
  • 3
  • 45
  • 66