How to replace certain rows by shared column values in pandas DataFrame?

ShanZhengYang

Let's say I have the following pandas DataFrame:

import pandas as pd

data = [['Alex',10],['Bob',12],['Clarke',13], ['Bob', '#'], ['Bob', '#'], ['Bob', '#']]

df = pd.DataFrame(data,columns=['Name','Age'], dtype=float)
print(df)
     Name Age
0    Alex  10
1     Bob  12
2  Clarke  13
3     Bob   #
4     Bob   #
5     Bob   #

So, there are odd rows in the DataFrame for Bob, namely rows 3, 4, and 5. These values are consistently #, not 12. Row 1 shows that Bob should be 12, not #.

In this example, it's straightforward to fix this with replace():

df = df.replace("#", 12)
print(df)
     Name Age
0    Alex  10
1     Bob  12
2  Clarke  13
3     Bob   12
4     Bob   12
5     Bob   12

However, this wouldn't work for larger dataframes, e.g.

     Name Age
0    Alex  10
1     Bob  12
2  Clarke  13
3     Bob   #
4     Bob   #
5     Bob   #
6  Clarke   #

whereby row 6 should be 6 Clarke 13.

How does one replace any row in Age with # with the correct integer as given in other rows, based on Name? If # exists, check other rows with the same Name value and replace #.

Mohamed Thasin ah

try this,

d= df[df['Age']!='#'].set_index('Name')['Age']
df['Age']=df['Name'].replace(d)

O/P:

     Name Age
0    Alex  10
1     Bob  12
2  Clarke  13
3     Bob  12
4     Bob  12
5     Bob  12
6  Clarke  13

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Replace column values in large Pandas dataframe

分類Dev

Replace values in dataframe column if second column matches a given list pandas

分類Dev

how to reorder of rows of a dataframe based on values in a column

分類Dev

How to Drop rows in DataFrame by conditions on column values

分類Dev

How to sum values of pandas dataframe by rows?

分類Dev

Replace pandas Dataframe column values with the list if it matches a word

分類Dev

Pandas select rows where relative column values exist in DataFrame

分類Dev

Pandas dataframe select entire rows with highest values from a specified column

分類Dev

Pandas Dataframe: Replace values in a dataframe with values from corresponding column vector of a different dataframe

分類Dev

How to drop all rows in pandas dataframe with negative values?

分類Dev

how to add a column based on values in two previous rows in pandas

分類Dev

How to replace string values in one column with actual column values from other columns in the same dataframe?

分類Dev

iterate to perform calculations on certain column in pandas dataframe

分類Dev

Missing values replace by med/mean in conti var, by mode in categorical var in pandas dataframe -after grouping the data by a column)

分類Dev

How to modify DataFrames so that they only have rows with shared index values in Pandas?

分類Dev

Check and replace column values in R dataframe

分類Dev

Moving rows from one column to another along with respective values in pandas DataFrame

分類Dev

How to replace all same values of a group in a column (dataframe) according to another column without loop?

分類Dev

How to create new values in a pandas dataframe column based on values from another column

分類Dev

How to replace certain values in a queryset with Django?

分類Dev

Replace and mapping string values in a Python dataframe with pandas

分類Dev

Combine Pandas DataFrame Rows by Timestamp and Column

分類Dev

Pandas Dataframe filter rows by only one column

分類Dev

Randomly assign values to subset of rows in pandas dataframe

分類Dev

Concatenate values from earlier rows in a pandas dataframe

分類Dev

Replace values in a column based on a dictionary (Pandas)

分類Dev

Replace a certain group in a pyspark dataframe column with a random item from a list

分類Dev

Excel VBA Merge Duplicate rows and sum values in certain column

分類Dev

Select rows where top n values of a certain column are distinct

Related 関連記事

  1. 1

    Replace column values in large Pandas dataframe

  2. 2

    Replace values in dataframe column if second column matches a given list pandas

  3. 3

    how to reorder of rows of a dataframe based on values in a column

  4. 4

    How to Drop rows in DataFrame by conditions on column values

  5. 5

    How to sum values of pandas dataframe by rows?

  6. 6

    Replace pandas Dataframe column values with the list if it matches a word

  7. 7

    Pandas select rows where relative column values exist in DataFrame

  8. 8

    Pandas dataframe select entire rows with highest values from a specified column

  9. 9

    Pandas Dataframe: Replace values in a dataframe with values from corresponding column vector of a different dataframe

  10. 10

    How to drop all rows in pandas dataframe with negative values?

  11. 11

    how to add a column based on values in two previous rows in pandas

  12. 12

    How to replace string values in one column with actual column values from other columns in the same dataframe?

  13. 13

    iterate to perform calculations on certain column in pandas dataframe

  14. 14

    Missing values replace by med/mean in conti var, by mode in categorical var in pandas dataframe -after grouping the data by a column)

  15. 15

    How to modify DataFrames so that they only have rows with shared index values in Pandas?

  16. 16

    Check and replace column values in R dataframe

  17. 17

    Moving rows from one column to another along with respective values in pandas DataFrame

  18. 18

    How to replace all same values of a group in a column (dataframe) according to another column without loop?

  19. 19

    How to create new values in a pandas dataframe column based on values from another column

  20. 20

    How to replace certain values in a queryset with Django?

  21. 21

    Replace and mapping string values in a Python dataframe with pandas

  22. 22

    Combine Pandas DataFrame Rows by Timestamp and Column

  23. 23

    Pandas Dataframe filter rows by only one column

  24. 24

    Randomly assign values to subset of rows in pandas dataframe

  25. 25

    Concatenate values from earlier rows in a pandas dataframe

  26. 26

    Replace values in a column based on a dictionary (Pandas)

  27. 27

    Replace a certain group in a pyspark dataframe column with a random item from a list

  28. 28

    Excel VBA Merge Duplicate rows and sum values in certain column

  29. 29

    Select rows where top n values of a certain column are distinct

ホットタグ

アーカイブ