2017-08-26 111 views
0

使用pandas將具有唯一過濾器的2個csv文件合併爲'NAME'。進一步嘗試將「STANCE」值與「兩足」相比較並打印。總結一下,想知道將任何列值與一個字符串進行比較的方法。將列值與字符串匹配

s1: 
       NAME LEG_LENGTH  DIET 
0  Hadrosaurus  1.20 herbivore 

s2: 
       NAME STRIDE_LENGTH  STANCE 
3  Hadrosaurus   1.40  bipedal 

merged: 
       NAME LEG_LENGTH  DIET STRIDE_LENGTH  STANCE 
0  Hadrosaurus  1.20 herbivore   1.40  bipedal 

代碼:

import pandas as pd 
import csv 

from collections import defaultdict 

csv1 = 'dataset1.csv' 
csv2 = 'dataset2.csv' 
g = 9.8 

def splits(c1, c2): 
    s1 = pd.read_csv(c1) 
    s2 = pd.read_csv(c2) 

    print '%s\n%s' % (s1,s2) 

    merged = s1.merge(s2, on="NAME", how = "outer") # Mergin two files on column NAME 
    print (merged) 
    return 

splits(csv1, csv2) 

回答

0

嘿,熊貓徒弟,嘗試

df.loc[df.STANCE.str.contains('bipedal')] 
+0

謝謝,但得到如下錯誤:如果(df.loc [df.STANCE.str.contains( 'bipedal')]): NameError:未定義全局名稱'df' – Shivanshu

+0

dude df是數據框的名稱 –