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:
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 ?
CodePudding user response:
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().