Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas one-liner to return column data of head output

I have a large DF with a list of projects and what phases are available for the project.

import pandas as pd
projects = {
    'phase': [1, 1, 1, 2, 1, 2, 1],
    'projID': ["foo","foo","foo","bar","foo","bar","foo"]
}
df = pd.DataFrame(projects)

I wish to return a list of all the project rows where the projID is "bar", but all I have is the phase number (2). So I have a lookup DF that lets me convert the data phase number to the projID:

lookup = {"phase":[1,2], "pname": ["bob","frank"]}
df_tmp = pd.DataFrame(lookup)
_data_phase = 2
pn = df_tmp.loc[df_tmp['phase'] == _data_phase ,'pname'].head(1)

Q1: I want pn to be the value ‘frank’ as a string, but instead it contains a DataFrame that look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1    frank
Name: pname, dtype: object

How do I convert that to just the string "frank"? And can that be done as a one-liner?

Q2: Is there a better way to extract the rows from project (df) where _data_phase = x ?

>Solution :

You can simply add the .values method or [0]. Kindly try:

pn = df_tmp.loc[df_tmp['phase'] == _data_phase ,'pname'].values[0]

I think that is slicing the dataframe with the specified criteria is a good to get what you need, I wouldn’t change it! (You can use other options such as df.query().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading