2017-09-25 103 views
1

我的目標是在查詢完整的用戶名/電子郵件信息後,從包含用戶名/ ipaddress或電子郵件/ ipaddress組合的csv文件合併列。這似乎是一個基本的內部連接,但不是從查詢輸出到CSV文件,需要結合工作根據數據框中的列合併兩個excel文件VLookup樣式

example row in original file: 
username  email     ip address 
NaN   [email protected]   69.0.1.127 
Wilco   NaN      69.0.12.128 

例如行:

username  email    branch 

roger1  [email protected]  Los Angelas 
Wilco  [email protected]  Montreal 

預期輸出:

username  email    branch   ipaddress 
roger1   [email protected]  Los Angelas  69.0.1.127 
Wilco   [email protected]  Montreal   69.0.12.128 

第一這個程序的一部分是從IP地址的Excel表中收集數據,用戶名或電子郵件地址爲 ,但不能同時填寫......所有這些都是將用戶名電子郵件並在該表格末尾輸入ipaddress,並創建該表格。

directory variable = dirname 

all_data = pd.DataFrame() 

emailList=[] 
userList = [] 
print "Dirname is " + dirname 
#loop over excel sheets and create email and user strings to use for query 
for f in glob.glob(dirname+"/Book1.xlsx"): 
    df=pd.read_excel(f) 
    all_data = all_data.append(df,ignore_index=True) 

    all_data = all_data.append(df,ignore_index=True) 
    emailList = all_data.dropna(subset=["email"]) 
    userList = all_data.dropna(subset=["user"]) 

    userList = userList["user"].tolist() 
    emailList = emailList["email"].tolist() 
    userList = map(int,userList) 
    userList = ','.join(map(str,userList)) 
    emailList = "', '".join(map(str, emailList)) 

    emailList = "'" + emailList.upper() + "'" 

all_data['email'] = all_data['email'].str.upper() 

查詢構建邏輯這裏<>

轉換電子郵件上和其他的語法爲下一步

con=cx_Oracle.connect("*************") 
print "connection successful" 
df_ora = pd.read_sql(queryStringEmail,con) 
df_ora2 = pd.read_sql(queryStringUserList, con) 
frames = [df_ora,df_ora2] 
con.close() 
newtable = pd.concat(frames) 

這給了我,我從需要的用戶列表的SQL查詢在程序中第一個excel工作表..所有其他行可以被忽略 ,除了在這個數據框輸出的用戶名/電子郵件。從第二csv文件

nt = newtable.drop_duplicates(keep='last') 

nt.to_csv("newcsv.csv", index=False, encoding='utf-8') 
print "Operation successful" 

刪除重複項爲同一用戶名/電子郵件/支連擊的多是無用

大量的意大利麪條代碼在這裏,我很抱歉,但是這是邏輯變得模糊

在新的CSV文件
for f in glob.glob(dirname+"/newcsv.csv"): 
    aa=pd.read_csv(f) 
all_data.to_csv("newcsvALLDATA.csv", index=False, encoding='utf-8') 
aa.to_csv("newcsvALLDATA2.csv", index=False, encoding='utf-8') 

重命名列投其所好,在原來的IP地址的CSV

aa.columns = ['user','email','first name','last name', 'branch', 'location'] 
print all_data 
print aa 
列(實際上只是電子郵件和用戶)

這是我已經嘗試了所有組合和失敗:

all_data = 

all_data.merge(aa,left_index=True,right_index=True,left_on="IP",how='inner') 

print all_data 
all_data.to_csv("newcsv2.csv", index=False, encoding='utf-8') 
aa.to_excel(writer, sheet_name = 'x2') 

我不能在瞬間打印數據,因爲我沒有訪問數據庫,但是可以在如果有人有任何想法稍後

同樣的目標是輸出相匹配的ip地址有兩種電子郵件 或用戶名列出,但不能同時

回答

0

櫃面有人運行到這個問題,在未來,我已經解決了CSV文件中的用戶名/電子郵件連擊csv文件它與以下代碼

aa.columns = ['user','email','first name','last name', 'provID', 'provName'] 
print aa 
all_dataMerge = pd.merge(aa,all_data, on='user', suffixes=['1','2']) 

合併發生在1個鍵上的兩個表之間,但它在邏輯上同時處理。簡單的答案,讓我永遠弄清楚。希望這有助於未來的人。

#Logic to combine the dataframes into final form, similar to VLookup 
    data_final = all_dataMerge.drop_duplicates(keep='first') 
相關問題