Home  >  Article  >  Backend Development  >  How Can I Modify Specific Values in Pandas DataFrame Columns Based on a Condition?

How Can I Modify Specific Values in Pandas DataFrame Columns Based on a Condition?

DDD
DDDOriginal
2024-10-26 03:34:27370browse

How Can I Modify Specific Values in Pandas DataFrame Columns Based on a Condition?

Altering Values Based on Conditions in Pandas

This question presents a situation where specific values in two columns, FirstName and LastName, need to be modified based on the condition of the ID column matching a particular value. In Stata, this can be achieved using simple replace commands.

In Pandas, a powerful Python library for data manipulation, one approach is to leverage the loc function with the indexing feature. This enables logical evaluation and subsequent data modification. For instance:

<code class="python">import pandas as pd

df = pd.read_csv("test.csv")
df.loc[df.ID == 103, 'FirstName'] = "Matt"
df.loc[df.ID == 103, 'LastName'] = "Jones"</code>

Alternatively, the assignment to both columns can be done in one step:

<code class="python">df.loc[df.ID == 103, ['FirstName', 'LastName']] = 'Matt', 'Jones'</code>

Note that Pandas version 0.11 or higher is required for loc overwrite assignment operations. For older versions, chained assignment is a viable solution:

<code class="python">df['FirstName'][df.ID == 103] = "Matt"
df['LastName'][df.ID == 103] = "Jones"</code>

While chained assignment should generally be avoided in modern Pandas versions, it remains a useful technique to be aware of for compatibility with older versions.

The above is the detailed content of How Can I Modify Specific Values in Pandas DataFrame Columns Based on a Condition?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn