How do I fill a string column using a set in Pandas dataframe?

rajnish chauhan

I have a huge dataset with two specific columns for Sales Person and Manager. I want to make a new column which assigns sales person name on different basis.

So lets say that Under Manager John, I have 4 executives - A, B, C, D

I want to replace the existing sales person under John with the executives A, B, C and D in a sequence.

Here is what I want to do -

Input-

ID SalesPerson Sales Manager
AM12 Oliver Bren
AM21 Athreyu John
AM31 Margarita Fer
AM41 Jenny Fer
AM66 Omar John
AM81 Michael Nati
AM77 Orlan John
AM87 Erika Nateran
AM27 Jesus John
AM69 Randy John

Output -

ID SalesPerson Sales Manager SalesPerson_new
AM12 Oliver Bren oliver
AM21 Athreyu John A
AM31 Margarita Fer Margarita
AM41 Jenny Fer Jenny
AM66 Omar John B
AM81 Michael Nati Michael
AM77 Orlan John C
AM87 Erika Nateran Nateran
AM27 Jesus John D
AM69 Randy John A
Manakin

We can do this with cumcount and .map

first we need to build up a dictionary that repeats ABCD in multiple of fours.

i.e {0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D', 4 : 'A'}

we can do this with a helper function and some handy modules from the itertools library.

from itertools import cycle, zip_longest, islice
from string import ascii_uppercase
import pandas as pd
import numpy as np

def repeatlist(it, count):
    return islice(cycle(it), count)

mapper = dict(zip_longest(range(50), repeatlist(ascii_uppercase[:4],50)))


df['SalesPersonNew'] = np.where(
df['Sales Manager'].eq('John'),
         df.groupby('Sales Manager')['SalesPerson'].cumcount().map(mapper),
         df['SalesPerson'])

print(df)

  ID SalesPerson Sales Manager SalesPersonNew
0  AM12      Oliver          Bren         Oliver
1  AM21     Athreyu          John              A
2  AM31   Margarita           Fer      Margarita
3  AM41       Jenny           Fer          Jenny
4  AM66        Omar          John              B
5  AM81     Michael          Nati        Michael
6  AM77       Orlan          John              C
7  AM87       Erika       Nateran          Erika
8  AM27       Jesus          John              D
9  AM69       Randy          John              A

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I fill a string column using a set in Pandas dataframe?

From Dev

How do I set ylim for subplots using Pandas dataframe?

From Dev

How do I only fill down when a column is 1 in Pandas DataFrame

From Dev

How do I set the time of a datetime column in python pandas based on a string column

From Dev

How do you add a calculated column to a pandas dataframe using set-based logic?

From Dev

How do you fill uneven pandas dataframe column with first value in column

From Dev

how do you filter a Pandas dataframe by a multi-column set?

From Dev

When using a pandas dataframe, how do I add column if does not exist?

From Dev

Using Pandas in Python 3, how do I filter out repeat strings in a column within a dataframe?

From Dev

How do I build a new column in pandas using the result of matching a string in column A on a regex pattern from column B?

From Dev

How do I take rows in Pandas Dataframe and transform into values for a Column?

From Dev

How do I remove/omit the count column from the dataframe in Pandas?

From Dev

How do I turn an array of column names into a pandas Dataframe?

From Dev

How do I copy rows in a pandas DataFrame and add an id column

From Dev

How do I "enrich" every record in a Pandas dataframe with an hour column?

From Dev

How do I properly set the Datetimeindex for a Pandas datetime object in a dataframe?

From Dev

How can I fill a DataFrame column with a list of e.g. 2 values in Pandas Python?

From Dev

How do I apply a regex substitution in a string column of a DataFrame?

From Java

How do I deconcatenate or unconcatenate a string value in a pandas dataframe?

From Dev

How do I split a string into several columns in a dataframe with pandas Python?

From Dev

How do I format a pandas dataframe string value to date?

From Dev

How do i fill the previous day's not null value in my pandas dataframe

From Dev

How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns?

From Java

How do I add a new column to a Spark DataFrame (using PySpark)?

From Dev

How can I search a pandas dataframe to fill in another dataframe?

From Dev

How do I set a column to search in using Excel VBA?

From Dev

How to set dtypes by column in pandas DataFrame

From Dev

How to fill in missing values in Pandas dataframe according to pattern in column?

From Dev

How can I fill some data of the cell of the new column that is in accord with a substring of the original data using pandas?

Related Related

  1. 1

    How do I fill a string column using a set in Pandas dataframe?

  2. 2

    How do I set ylim for subplots using Pandas dataframe?

  3. 3

    How do I only fill down when a column is 1 in Pandas DataFrame

  4. 4

    How do I set the time of a datetime column in python pandas based on a string column

  5. 5

    How do you add a calculated column to a pandas dataframe using set-based logic?

  6. 6

    How do you fill uneven pandas dataframe column with first value in column

  7. 7

    how do you filter a Pandas dataframe by a multi-column set?

  8. 8

    When using a pandas dataframe, how do I add column if does not exist?

  9. 9

    Using Pandas in Python 3, how do I filter out repeat strings in a column within a dataframe?

  10. 10

    How do I build a new column in pandas using the result of matching a string in column A on a regex pattern from column B?

  11. 11

    How do I take rows in Pandas Dataframe and transform into values for a Column?

  12. 12

    How do I remove/omit the count column from the dataframe in Pandas?

  13. 13

    How do I turn an array of column names into a pandas Dataframe?

  14. 14

    How do I copy rows in a pandas DataFrame and add an id column

  15. 15

    How do I "enrich" every record in a Pandas dataframe with an hour column?

  16. 16

    How do I properly set the Datetimeindex for a Pandas datetime object in a dataframe?

  17. 17

    How can I fill a DataFrame column with a list of e.g. 2 values in Pandas Python?

  18. 18

    How do I apply a regex substitution in a string column of a DataFrame?

  19. 19

    How do I deconcatenate or unconcatenate a string value in a pandas dataframe?

  20. 20

    How do I split a string into several columns in a dataframe with pandas Python?

  21. 21

    How do I format a pandas dataframe string value to date?

  22. 22

    How do i fill the previous day's not null value in my pandas dataframe

  23. 23

    How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns?

  24. 24

    How do I add a new column to a Spark DataFrame (using PySpark)?

  25. 25

    How can I search a pandas dataframe to fill in another dataframe?

  26. 26

    How do I set a column to search in using Excel VBA?

  27. 27

    How to set dtypes by column in pandas DataFrame

  28. 28

    How to fill in missing values in Pandas dataframe according to pattern in column?

  29. 29

    How can I fill some data of the cell of the new column that is in accord with a substring of the original data using pandas?

HotTag

Archive