User Tools

Site Tools


pandas_dataframe

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
pandas_dataframe [2023/07/21 22:16] – [Create a dataframe from list of lists] rajupandas_dataframe [2023/09/07 21:45] (current) – [lookup value] raju
Line 283: Line 283:
 tags | uses [http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.split.html pandas.Series.str.split] tags | uses [http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.split.html pandas.Series.str.split]
  
 +==== lookup value ====
 +To pick the first value in column 'A' for rows where column B is FOO
 +<code>
 +df.loc[df['B'] == 'FOO', 'A'].iloc[0]
 +</code>
 +
 +Example:
 +<code>
 +$ ipython
 +In [1]:
 +import pandas as pd
 +df = pd.DataFrame({'A': ['p1', 'p2', 'p3', 'p4'], 'B': [1, 3, 3, 2]})
 +print(df)
 +    A  B
 +0  p1  1
 +1  p2  3
 +2  p3  3
 +3  p4  2
 +
 +In [2]:
 +df.loc[df['B'] == 3, 'A']
 +Out[2]:
 +1    p2
 +2    p3
 +Name: A, dtype: object
 +
 +In [3]:
 +df.loc[df['B'] == 3, 'A'].iloc[0]
 +Out[3]:
 +'p2'
 +</code>
 +
 +search tags | value of one column when another column equals something
 +
 +Ref:- https://stackoverflow.com/questions/36684013/extract-column-value-based-on-another-column-in-pandas
  
 ===== Series related ===== ===== Series related =====
pandas_dataframe.txt · Last modified: 2023/09/07 21:45 by raju