Skip to content

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

@pinshuai

Description

@pinshuai
  • 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.

Metadata

Metadata

Assignees

Labels

Closing CandidateMay be closeable, needs more eyeballsDuplicate ReportDuplicate issue or pull requestNeeds TriageIssue that has not been reviewed by a pandas team memberUsage Question

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions