【发布时间】:2021-03-25 05:35:54
【问题描述】:
我需要根据我在列表中提取的另一列的值从数据框中的列中提取值。
import pandas as pd
data = [[1, 'john', 'kelly'], [2, 'john', 'raj'], [2, 'john', 'leonard'], [3, 'penny', 'stuart'], [3, 'penny', 'halley'], [3, 'penny', 'amy'], [4, 'sheldon', 'will'], [4, 'sheldon', 'richard']]
school = pd.DataFrame(data, columns=['teacher_id', 'teacher_name', 'student_name'])
print(school)
这是我的数据框。
teacher_id teacher_name student_name
0 1 john kelly
1 2 john raj
2 2 john leonard
3 3 penny stuart
4 3 penny halley
5 3 penny amy
6 4 sheldon will
7 4 sheldon richard
从这个数据框中,我提取了出现次数最多的teacher_id。
school.teacher_id.value_counts().head()
> 3 3
2 2
4 2
1 1
现在使用上面的值(teacher_id)我怎样才能得到老师的名字?
【问题讨论】:
-
试试
s=pd.DataFrame(school.teacher_id.value_counts().head()) s.merge(school.iloc[:,:2], how='left', on='teacher_id').drop_duplicates(keep='first')#get dataframe或s.merge(school.iloc[:,:2], how='left', on='teacher_id').drop_duplicates(keep='first')['teacher_name'].to_list()#put to list
标签: python pandas dataframe data-extraction