Home  >  Article  >  Backend Development  >  How to Efficiently Add a Mapped Column to a Pandas DataFrame?

How to Efficiently Add a Mapped Column to a Pandas DataFrame?

DDD
DDDOriginal
2024-10-29 02:27:29208browse

How to Efficiently Add a Mapped Column to a Pandas DataFrame?

Adding a Mapped Column to a Pandas DataFrame

When working with pandas, adding a new column with a mapped value based on an existing column can be a straightforward task. However, certain approaches may result in errors or difficulties.

One common attempt is to directly assign the mapped value to the new column:

<code class="python">df["B"] = equiv(df["A"])</code>

However, this will fail as equiv, representing a dictionary, is not a callable function.

Another approach that may not yield the desired result is using map with a lambda function:

<code class="python">df["B"] = df["A"].map(lambda x: equiv[x])</code>

This expression will likely raise a KeyError unless the dictionary keys exactly match the column values.

The Correct Solution

The proper method to add a mapped column is to use map directly with the dictionary:

<code class="python">df["B"] = df["A"].map(equiv)</code>

This approach will create a new column, B, with the mapped values from the equiv dictionary. If a key does not exist in the dictionary, the corresponding row will be assigned NaN.

Example

Consider the following DataFrame:

<code class="python">df = pd.DataFrame({"A": [7001, 8001, 9001]})
equiv = {7001: 1, 8001: 2, 9001: 3}</code>

Applying the correct mapping will produce the desired result:

<code class="python">df["B"] = df["A"].map(equiv)

print(df)

      A  B
0  7001  1
1  8001  2
2  9001  3</code>

The above is the detailed content of How to Efficiently Add a Mapped Column to a Pandas DataFrame?. 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