Dataframe updates with pandas that includes duplicated column headers

DuckCowMooQuack

I am incredibly new to pandas python module and have a problem I'm trying to solve. Take the following dataframe as an example. This was read in from a .csv where "link" is the column header for the last three columns:

  summary       link     link.1     link.2
0    test  PCR-12345  PCR-54321  PCR-65432
1   test2        NaN        NaN        NaN
2   test3    DR-1234   PCR-1244        NaN
3   test4   PCR-4321    DR-4321        NaN

My goal is to update the dataframe to the following:

  summary       link     link.1     link.2
0    test        NaN        NaN        NaN
1   test2        NaN        NaN        NaN
2   test3    DR-1234        NaN        NaN
3   test4        NaN    DR-4321        NaN

So the criteria is basically, if the column header is "link.X" AND the value contains a string that starts with "PCR-", update it to an empty/NaN value.

How do I loop through each row's values, check the header and value, and replace if criteria is satisfied?

Quang Hoang

Let's try pd.Series.str.startswith and pd.Series.mask:

# columns starting with `link`
cols = df.columns[df.columns.str[:4]=='link']

# for each `link` column, mask the `PCR` with `NaN`:
df[cols] = df[cols].apply(lambda x: x.mask(x.str.startswith('PCR')==True) )

Output:

  summary     link   link.1 link.2
0    test      NaN      NaN    NaN
1   test2      NaN      NaN    NaN
2   test3  DR-1234      NaN    NaN
3   test4      NaN  DR-4321    NaN

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas.Dataframe.duplicated() includes missing rows as duplicates

From Java

Get list from pandas DataFrame column headers

From Dev

Adding Column Headers to new pandas dataframe

From Java

How can I make pandas dataframe column headers all lowercase?

From Dev

How can I transform a pandas dataframe into a dictionary without the column headers?

From Dev

Trim part of string from column headers in a pandas dataframe

From Dev

Pandas dataframe - Rearranging row index values into column headers

From Dev

Pandas: multiindexing column headers

From Dev

Make a column with duplicated values unique in a dataframe

From Dev

Fastest Way to Drop Duplicated Index in a Pandas DataFrame

From Dev

Sort pandas DataFrame by multiple columns and duplicated index

From Dev

Python Pandas Identify Duplicated rows with Additional Column

From Java

Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

From Dev

Setting DataFrame column headers to a MultiIndex

From Dev

Is it possible to multiple updates across rows based on a query on single pandas DataFrame column

From Dev

Cleaning headers in imported pandas dataframe

From Dev

Nested List to Pandas Dataframe with headers

From Dev

Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers

From Dev

Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers

From Dev

Create new dictionary column in Pandas dataframe of all the other columns combined with their headers

From Dev

Change column values to column headers in pandas

From Dev

R - reshape dataframe from duplicated column names but unique values

From Dev

Subsetting rows of a dataframe when respondent number is duplicated in column

From Dev

Pandas how to reshape a dataframe containing duplicated values for columns

From Dev

Sum duplicated rows on a multi-index pandas dataframe

From Dev

Using JSON schema as column headers in dataframe

From Dev

Column to row in pandas dataframe

From Dev

move column in pandas dataframe

From Java

Append column to pandas dataframe

Related Related

  1. 1

    Pandas.Dataframe.duplicated() includes missing rows as duplicates

  2. 2

    Get list from pandas DataFrame column headers

  3. 3

    Adding Column Headers to new pandas dataframe

  4. 4

    How can I make pandas dataframe column headers all lowercase?

  5. 5

    How can I transform a pandas dataframe into a dictionary without the column headers?

  6. 6

    Trim part of string from column headers in a pandas dataframe

  7. 7

    Pandas dataframe - Rearranging row index values into column headers

  8. 8

    Pandas: multiindexing column headers

  9. 9

    Make a column with duplicated values unique in a dataframe

  10. 10

    Fastest Way to Drop Duplicated Index in a Pandas DataFrame

  11. 11

    Sort pandas DataFrame by multiple columns and duplicated index

  12. 12

    Python Pandas Identify Duplicated rows with Additional Column

  13. 13

    Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

  14. 14

    Setting DataFrame column headers to a MultiIndex

  15. 15

    Is it possible to multiple updates across rows based on a query on single pandas DataFrame column

  16. 16

    Cleaning headers in imported pandas dataframe

  17. 17

    Nested List to Pandas Dataframe with headers

  18. 18

    Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers

  19. 19

    Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers

  20. 20

    Create new dictionary column in Pandas dataframe of all the other columns combined with their headers

  21. 21

    Change column values to column headers in pandas

  22. 22

    R - reshape dataframe from duplicated column names but unique values

  23. 23

    Subsetting rows of a dataframe when respondent number is duplicated in column

  24. 24

    Pandas how to reshape a dataframe containing duplicated values for columns

  25. 25

    Sum duplicated rows on a multi-index pandas dataframe

  26. 26

    Using JSON schema as column headers in dataframe

  27. 27

    Column to row in pandas dataframe

  28. 28

    move column in pandas dataframe

  29. 29

    Append column to pandas dataframe

HotTag

Archive