pandas apply only returning first value when using logical indexing

spacediver

I create two dataframes:

data = [['John'], ['Mary']]
df1 = pd.DataFrame(data, columns = ['Name'])
df1['Height'] = 0

data = [['John', 5], ['Mary', 6]]
df2 = pd.DataFrame(data, columns = ['Name', 'Height'])

df1

Output:

       Name  Height
    0  John  0
    1  Mary  0

df2

Output:
       Name  Height
    0  John  5
    1  Mary  6

Now I try to fill in df1's Height using the values from df2:

df1['Height'] = df1.apply(lambda row: df2[df2.Name == row.Name]['Height'], axis = 1)

df1

Output:
       Name  Height
    0  John  5
    1  Mary  Nan

Why does only the first name (John) have the Height filled in? Shouldn't apply() be iterating through all the rows of the df1 and returning the Height from df2 where df2 matches the name in the current row of df1?

Quang Hoang

The problem is that df2[df2.Name == row.Name]['Height'] returns a series with different indexes. You when Pandas concatenate these series, it yields different columns. In particular:

df1.apply(lambda row: df2[df2.Name == row.Name]['Height'], axis = 1)

returns:

     0    1
0  5.0  NaN
1  NaN  6.0

and it looks like Pandas takes the first column to assign when you do:

df['Height'] = ...

To fix your code, you need to extract the single value:

df1['Height'] = df1.apply(lambda row: df2[df2.Name == row.Name]['Height'].iloc[0], axis = 1)

However, this is certainly not the best way to approach the problem. You should either take a look at map or merge. For example:

df1['Height'] = df1['Name'].map(df2.set_index('Name')['Height'])

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

regex scan only returning first value

分類Dev

setState Concat storing/returning only First value-- React Native

分類Dev

Array value only returning first character and extra values

分類Dev

Logical indexing in JAVA

分類Dev

redissonClient.poll() only returning the first 8 characters of String type value

分類Dev

The parameter in a custom function when using pandas.Series.apply

分類Dev

Using RXJS to issue two sequential http calls and returning the result of the first one only

分類Dev

Select statement only returning first row in result

分類Dev

scraper only returning results for first 2 inputs

分類Dev

Masking/modifying values using advanced indexing with pandas

分類Dev

specify a first column when exporting csv using pandas

分類Dev

Several errors in Swift when using || or && logical operators

分類Dev

apply prepends space for logical

分類Dev

Why does the cell style index returning wrong CellFormat value when parsing excel cells using OpenXml SDK?

分類Dev

Using QualifierFilter leads to only returning matched columns

分類Dev

Example to clarify fill_value when using add() on dataframes (pandas)

分類Dev

IBM Watson Speech to Text Only Returning First Word With Java SDK

分類Dev

Python's Popen + communicate only returning the first line of stdout

分類Dev

Compiling Sass with Scout doesn't apply correct alpha value when using rgba(r,g,b,a)

分類Dev

Checkbox not returning value when not checked PHP Codeigniter

分類Dev

How to rename a logical value (TRUE or FALSE) with "Yes" or "No" and apply distinct() to FALSE values

分類Dev

python pandas - groupby.first() returning NaT values

分類Dev

How to set a defaultChecked to only first input in radio button when using map function in reactjs

分類Dev

Pandas read_csv only first comma

分類Dev

ListView using findViewById is returning null value

分類Dev

Correct Use of Matcher Groups in Java Regex when using logical OR

分類Dev

returning the first column value with two independent row criteria in excel

分類Dev

R: passing by parameter to function and using apply instead of nested loop and recursive indexing failed

分類Dev

pandas using apply method and sending column names?

Related 関連記事

  1. 1

    regex scan only returning first value

  2. 2

    setState Concat storing/returning only First value-- React Native

  3. 3

    Array value only returning first character and extra values

  4. 4

    Logical indexing in JAVA

  5. 5

    redissonClient.poll() only returning the first 8 characters of String type value

  6. 6

    The parameter in a custom function when using pandas.Series.apply

  7. 7

    Using RXJS to issue two sequential http calls and returning the result of the first one only

  8. 8

    Select statement only returning first row in result

  9. 9

    scraper only returning results for first 2 inputs

  10. 10

    Masking/modifying values using advanced indexing with pandas

  11. 11

    specify a first column when exporting csv using pandas

  12. 12

    Several errors in Swift when using || or && logical operators

  13. 13

    apply prepends space for logical

  14. 14

    Why does the cell style index returning wrong CellFormat value when parsing excel cells using OpenXml SDK?

  15. 15

    Using QualifierFilter leads to only returning matched columns

  16. 16

    Example to clarify fill_value when using add() on dataframes (pandas)

  17. 17

    IBM Watson Speech to Text Only Returning First Word With Java SDK

  18. 18

    Python's Popen + communicate only returning the first line of stdout

  19. 19

    Compiling Sass with Scout doesn't apply correct alpha value when using rgba(r,g,b,a)

  20. 20

    Checkbox not returning value when not checked PHP Codeigniter

  21. 21

    How to rename a logical value (TRUE or FALSE) with "Yes" or "No" and apply distinct() to FALSE values

  22. 22

    python pandas - groupby.first() returning NaT values

  23. 23

    How to set a defaultChecked to only first input in radio button when using map function in reactjs

  24. 24

    Pandas read_csv only first comma

  25. 25

    ListView using findViewById is returning null value

  26. 26

    Correct Use of Matcher Groups in Java Regex when using logical OR

  27. 27

    returning the first column value with two independent row criteria in excel

  28. 28

    R: passing by parameter to function and using apply instead of nested loop and recursive indexing failed

  29. 29

    pandas using apply method and sending column names?

ホットタグ

アーカイブ