question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

QST: why changing dataframe values impacts the array that the dataframe was created from?

See original GitHub issue
  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.


Question about pandas

>>> import numpy as np
>>> import pandas as pd

>>> mat = np.array([[1,2], [2,3]])
>>> mat
array([[1, 2],
       [2, 3]])

>>> df = pd.DataFrame(mat)
>>> df.iloc[0,1] = 4

>>> mat
array([[1, 4],
       [2, 3]])

Why the value in the original mat is also changed? I am not sure if this is a bug or Pandas’ way of doing things. I found it to be very confusing.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
attack68commented, Jul 20, 2021

closing - democracy seems to have identified this as a non-issue, but rather an educational question for which an answer was given

1reaction
rhshadrachcommented, Jul 18, 2021

@Varun270 the relevant code will be in pandas.core.frame or pandas.core.generic. However, it seems to me there is no issue to be resolved here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Will changes in DataFrame.values always modify the ...
It's an implementation detail if a copy is returned (then changing the values would not change the DataFrame) or if values returns a...
Read more >
pandas.DataFrame — pandas 0.21.1 documentation
Assign new columns to a DataFrame, returning a new object (a copy) with all the original columns in addition to the new ones....
Read more >
12 Useful Pandas Techniques in Python for Data Manipulation
Introduction · Let's get started · #1 – Boolean Indexing in Pandas · #2 – Apply Function in Pandas · #3 – Imputing...
Read more >
How to Replace Values in Pandas DataFrame - Data to Fish
(2) Replace multiple values with a new value for an individual DataFrame column: df['column name'] = df['column name'].replace(['1st old ...
Read more >
10 Data manipulation with pandas | Python for economists
When the dataframe was created, the GDP value for Germany was therefore assigned as NaN . 10.1.2.5 Creation of Dataframes from a Two-dimensional...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found