如何从Python的数据框中选择特定的细节?

时间:2019-12-04 02:15:50

标签: python dataframe

我想在Python中创建一个新对象,该对象与数据框中女性成员的名字和姓氏相对应。

import numpy as np
import pandas as pd
Index = np.array(['B1','L1','T1','G1','L2'])
Name =  pd.Series(['Bob','Linda','Tina','Gene','Louise'] , index = Index )
Gender = pd.Series(['Male','Female','Female','Male','Female'] , index = Index )
Age = pd.Series([46,42,13,11,9] , index = Index )
Occupation = pd.Series(['Chef','Restaurateur','Student','Student','Student'] , index = Index )
Rating = pd.Series([7,6,9,9,8] , index = Index )

burger = pd.DataFrame(data=dict(Name=Name,Gender=Gender,Age=Age,Occupation=Occupation,Rating=Rating))
burger

Picture of Data

我知道以下代码是如何从系列中选择某些东西的,但是我不确定如何处理数据帧。

burger.loc[]
burger.iloc[]

1 个答案:

答案 0 :(得分:1)

尝试一下,输出是一个数据帧

df = burger[burger.Gender=="Female"][["Name"]]

如果需要系列作为输出

df = burger[burger.Gender=="Female"]["Name"]