Home > Article > Backend Development > Pandas: When Does Selecting from a DataFrame Create a View vs. a Copy?
Pandas: Understanding the Rules for Generating Views vs Copies
Confusion in Identifying Views and Copies
Determining whether a selection from a Pandas DataFrame results in a view or a copy can be puzzling. This confusion arises primarily from the variety of indexing operations and their varying behaviors.
General Rules
Specific Examples
Assigning values to a comparison:
<code class="python">df[df.C <= df.B] = 7654321</code>
This assignment modifies the original DataFrame df because the indexer .loc is used to set values in-place.
Chained indexing:
<code class="python">df[df.C <= df.B].loc[:, 'B':'E']</code>
This operation is discouraged as it may not be reliable. To avoid potential issues, use the following syntax instead:
<code class="python">df.loc[df.C <= df.B, 'B':'E']</code>
Modifying Values Based on a Query
To modify all values in a DataFrame that meet a specific query condition, use the loc indexer with the query condition as an argument. For example:
<code class="python">df.loc[df.C <= df.B, 'E'] = 40</code>
This assignment will only change the values in column 'E' for rows where df.C is less than or equal to df.B.
The above is the detailed content of Pandas: When Does Selecting from a DataFrame Create a View vs. a Copy?. For more information, please follow other related articles on the PHP Chinese website!