2017-02-27 61 views
0

的SQL操作的SQL操作如下:添加新列基于类似蟒蛇大熊猫

UPDATE table_A s SET t.stat_fips=s.stat_fips 
    WHERE t.stat_code=s.stat_code; 

如果需要在CSV完成从CSV B A比较一些价值类似的操作如何实现这个在Python中?

数据: 让我们假设 -

CSV A 
col1 stat_code name 
abc  WY   ABC 
def  NA   DEF 
ghi  AZ   GHI 

CSV B 
stat_fips stat_code 
2234  WY 
4344  NA 
4588  AZ 


Resulting CSV : 

col1 stat_code name stat_fips 
abc  WY  ABC  2234 
def  NA  DEF  4344 
ghi  AZ  GHI  4588

添加试图到目前为止的代码:(没有真正的大熊猫一定了解基础知识还)

df = pd.read_csv('fin.csv',sep='\t', quotechar="'") 
    df = df.set_index('col1').stack(dropna=False).reset_index 
    df1['stat_fips'] = df1['stat_code'] 
    print df1 

+0

DF = pd.read_csv( 'fin.csv' ,'sep ='\ t',quotechar =“'”) df = df.set_index('col1')。stack(dropna = False).reset_index() df1 ['stat_fips'] = df1 ['stat_code'] print df – Viv

回答

2

看你的数据。例如,这看起来像合并操作上的stat_code柱:

import pandas as pd 

df_a = pd.DataFrame([["abc", "WY", "ABC"], ["def", "NA", "DEF"]], columns= ["col1", "stat_code", "name"]) 
df_b = pd.DataFrame([[2234, "WY"], [4344, "NA"]], columns=["stat_fips", "stat_code"]) 

merged_df = pd.merge(df_a, df_b, on="stat_code", how="left") 
print(merged_df) 

    col1 stat_code name stat_fips 
0 abc  WY ABC  2234 
1 def  NA DEF  4344 
+0

如何确保stat_fips保持int而不是字符串? – Viv

+0

合并不会将您的'stat_fips'从int更改为字符串,因此您不必担心这一点。合并发生之前,您可能已经为'stat_fips'创建了字符串。 – pansen

+0

虽然它显示为str类型!我需要对csv中生成的新列执行int操作,但它失败。 – Viv

2

看来你需要map作者:dictd

d = df2.set_index('stat_code')['stat_fips'].to_dict() 
df1['stat_fips'] = df1['stat_code'].map(d) 
print (df1) 

    col1 stat_code name stat_fips 
0 abc  WY ABC  2234 
1 def  NaN DEF  4344 
2 ghi  AZ GHI  4588 

或者merge与左连接:

df3 = pd.merge(df1, df2, on='stat_code', how='left') 
print (df3) 

    col1 stat_code name stat_fips 
0 abc  WY ABC  2234 
1 def  NaN DEF  4344 
2 ghi  AZ GHI  4588 
+0

如何更改最后的c olumn类型为int而不是字符串? – Viv

+0

使用'df3。 stat_fips = df3。 stat_fips.astype(int)' – jezrael

+0

ValueError:无法将NA转换为整数,以前的命令如果我尝试查看它是str的此列的类型。我很肯定 – Viv