“groupby.unique”和“groupby.first”函数之间的区别

2024-10-02 16:25:09 发布

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

我尝试在一个数据帧中组合两个具有相同邮政编码的邻居名称

最初我使用groupby.unique()函数,但它在每个单元格中返回一个ndarray。后来我尝试了groupby.first()函数,它返回重复的邮政编码的值

PCode =pd.DataFrame({'PostalCode':['M4A','M5A','M5A'],'Borough':['North York','Downtown Toronto','Downtown Toronto'],'Neighbourhood':['Victoria Village','Harbourfront','Regent Park']})
PCode.groupby('PostalCode')['PostalCode'].unique()
PCode.groupby('PostalCode')['PostalCode'].first()

我想知道是否有办法从返回的ndarray中提取邮政编码,以便groupby.unique()函数产生与使用groupby.first()函数时完全相同的结果


Tags: 数据函数名称dataframefirstpdunique邮政编码
1条回答
网友
1楼 · 发布于 2024-10-02 16:25:09

I wonder there is a way to extract the postal code out of the returned ndarray

使用str[0]

print(PCode.groupby('PostalCode')['PostalCode'].unique().str[0])

PostalCode
M4A    M4A
M5A    M5A

相关问题 更多 >