Replace NaN's in one column with string, based on value in another column

zbinsd

Simply, where column B = 't3', I want to replace the NaN value in column A with a new string.

My attempts below have all failed.

d = pd.DataFrame({"A":[np.nan, 't2', np.nan, 't3', np.nan], "B":['t1', 't2', 't3', 't4', 't3']})
print "Original Dataframe:\n", d

# Does not work
d[d.B == 't3'].A = 'new_val'

# Does not work
d[d.B == 't3'].A.replace(np.nan, 'new_val')


# Does not work
d[d.B == 't3'].A.replace(np.nan, 'new_val', inplace=True)

print "Final Dataframe:\n", d

Here's the output:

Original Dataframe:
     A   B
0  NaN  t1
1   t2  t2
2  NaN  t3
3   t3  t4
4  NaN  t3

[5 rows x 2 columns]
Final Dataframe:
     A   B
0  NaN  t1
1   t2  t2
2  NaN  t3
3   t3  t4
4  NaN  t3
EdChum

Use loc see http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing-loc-iloc-and-ix

In [5]:

d.loc[(d['A'].isnull()) & (d.B == 't3'), 'A']='new_val'

d

Out[5]:

         A   B
0      NaN  t1
1       t2  t2
2  new_val  t3
3       t3  t4
4  new_val  t3

[5 rows x 2 columns]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Pandas/Python: Set value of one column based on value in another column

From Dev

SELECT Mysql - Replacing value in one column based on another column

From Dev

How to assign a default value in a column based on another column's value?

From Dev

Copy value from one column based on the value of another column

From Dev

Updating one column based on the value of another column

From Dev

Replace NA's belonging to one column with values from another column

From Dev

Flag a row in one column based on past value in another column

From Dev

Pandas assign value of one column based on another

From Dev

how to highlight sequential string in one column based on another column

From Dev

How to replace values in a column if another column is a NaN?

From Dev

groupby and withhold information of one column based on value of another column

From Dev

Pandas: map dictionary values on an existing column based on key from another column to replace NaN

From Dev

Pandas/Python: Set value of one column based on value in another column

From Dev

Replace one column with another except another is NaN in Pandas

From Dev

Replace one string and create a column with another

From Dev

How to replace or convert a SQLite column string value with another string value?

From Dev

Replace NaN's in one column with string, based on value in another column

From Dev

How to assign a default value in a column based on another column's value?

From Dev

Replace column value with another column's value conditionally perl

From Dev

Change value of a column based on whether string is in another column using R

From Dev

Copy value from one column based on the value of another column

From Dev

replace text based on another column value

From Dev

how to highlight sequential string in one column based on another column

From Dev

Updating database column with string built based on value of another column

From Dev

Replace (A) column's value based on another (B) column's value in Microsoft PowerBI

From Dev

Replace values in pandas column based on nan in another column

From Dev

Setting the value of one column based on three conditions of another column

From Dev

set a value to column in one table with another table's column data

From Dev

Replace second occurrence of a string in one column based on value in other column in R

Related Related

  1. 1

    Pandas/Python: Set value of one column based on value in another column

  2. 2

    SELECT Mysql - Replacing value in one column based on another column

  3. 3

    How to assign a default value in a column based on another column's value?

  4. 4

    Copy value from one column based on the value of another column

  5. 5

    Updating one column based on the value of another column

  6. 6

    Replace NA's belonging to one column with values from another column

  7. 7

    Flag a row in one column based on past value in another column

  8. 8

    Pandas assign value of one column based on another

  9. 9

    how to highlight sequential string in one column based on another column

  10. 10

    How to replace values in a column if another column is a NaN?

  11. 11

    groupby and withhold information of one column based on value of another column

  12. 12

    Pandas: map dictionary values on an existing column based on key from another column to replace NaN

  13. 13

    Pandas/Python: Set value of one column based on value in another column

  14. 14

    Replace one column with another except another is NaN in Pandas

  15. 15

    Replace one string and create a column with another

  16. 16

    How to replace or convert a SQLite column string value with another string value?

  17. 17

    Replace NaN's in one column with string, based on value in another column

  18. 18

    How to assign a default value in a column based on another column's value?

  19. 19

    Replace column value with another column's value conditionally perl

  20. 20

    Change value of a column based on whether string is in another column using R

  21. 21

    Copy value from one column based on the value of another column

  22. 22

    replace text based on another column value

  23. 23

    how to highlight sequential string in one column based on another column

  24. 24

    Updating database column with string built based on value of another column

  25. 25

    Replace (A) column's value based on another (B) column's value in Microsoft PowerBI

  26. 26

    Replace values in pandas column based on nan in another column

  27. 27

    Setting the value of one column based on three conditions of another column

  28. 28

    set a value to column in one table with another table's column data

  29. 29

    Replace second occurrence of a string in one column based on value in other column in R

HotTag

Archive