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

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

DDD
Release: 2024-10-29 02:27:29
Original
396 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template