简体   繁体   English

如何比较两个数据集并提取值

[英]How to compare two data sets and extract values

I require to compare and extract the values between two dataset:我需要比较和提取两个数据集之间的值:

Data set 1:数据集 1:

names=["station", "phase", "hour", "minute", "second"]
arr = pd.read_csv('arrival.txt',sep='\t',names=names)
station= arr[['st', 'phase']]
        ST    Phs
0      UCP    EP
1      CET    ES
2      AET    EP
3      YHL    ES
4      DHL    EP
5      UAG    ES
6      FAG    EP
7      PUR    ES
8      MUR    EP
9      RBB    ES
10     NBB    EP

Data set 02数据集02

names=["station", "long2", "lat2"]
st = pd.read_csv('station.txt',sep='\t',names=names)


     station     long2      lat2
0      CET  81.91500  56.82850
1      UCP  72.32200  37.16267
2      PUR  22.57900  93.61317
3      RUR  63.44883  77.83300
4      AET  11.52967  63.15267
5      PIL  73.25167  34.64967
6      NRB  13.27017  68.65167
7      WPR  83.14017  11.75200
8      TAL  72.46550  32.63183

WHAT REQUIRED: I need to compare each entry to the first column of dataset 1 with the first column of dataset 2. If it matches then we pick the corresponding values from the second column and then store as:需要什么:我需要将每个条目与数据集 1 的第一列与数据集 2 的第一列进行比较。如果匹配,则我们从第二列中选择相应的值,然后存储为:

UCP    EP   72.32200  37.16267
CET    ES   81.91500  56.82850 

Try this:尝试这个:

st.merge(station, left_on='station', right_on='st', how='inner').drop(['st'],axis=1)

or或者

st.set_index('station').merge(station, left_index=True, right_on='st')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM