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

2024-06-28 14:47:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在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[]

Tags: 数据对象nameimportageindexgenderstudent
1条回答
网友
1楼 · 发布于 2024-06-28 14:47:20

试试这个,输出是一个数据帧

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

如果您需要一个系列作为输出

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

相关问题 更多 >