如何判断一个数据是否在一个表格里面,如果存在提取相关数据
import pandas as pdimport numpy as np
name = {"name":['林莽','黄强辉','章汉夫']}
df1 = pd.DataFrame(name)
name =['高大山','马宏宇','林莽','章汉夫','林君雄']
col=["python","JAVA","C"]
df2 = pd.DataFrame(np.random.randint(0,100,size=(5, 3)),columns=col)
df2.insert(0,"name",name)
如果df1的名字存在于df2,将df2对应的行里面的数据输出到字典里面 "内连接" merge()即可
>>> df1 = pd.DataFrame({'name':['Alice', 'Bill', 'Cat']})
>>> df1
name
0Alice
1 Bill
2 Cat
>>> df2 = pd.DataFrame(np.random.randint(0,100, size=(5,3)), columns=['python','java', 'C'])
>>> df2.insert(0, 'name', ['Alice', 'Bill', 'David', 'John', 'Jack'])
>>> df2
namepythonjava C
0Alice 32 619
1 Bill 6 9699
2David 60 6936
3 John 89 8645
4 Jack 68 3380
>>> df1.merge(df2, how='inner', on='name')
namepythonjava C
0Alice 32 619
1 Bill 6 9699
>>>
页:
[1]