Моя цель - импортировать два файла excel, у меня есть история номера телефона, у другого есть некоторые рабочие номера.
Я хочу сравнить номера работы с номерами телефонов в истории номеров телефонов и сохранить их в новой матрице с указанием даты и соответствующей продолжительности телефонного звонка.
На данный момент я делаю это вручную, как показано ниже, может кто-то мне помочь?
Спасибо всем.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt; plt.rcdefaults()
import os
clear = lambda:os.system('cls')
clear()
xls = pd.ExcelFile("C:\ - location")
df1 = pd.read_excel(xls, 'RawData', dtype= {'Date':np.datetime64, 'Type':str}, header=None)
df2 = pd.read_excel(xls, 'WorkNumbers',0)
dR = df1.as_matrix()
dWN = df2.as_matrix()
Ewen = df1[(df1['Number'] == #mobile number#)]
Alex = df1[(df1['Number'] == #mobile number#)]
Nirmal = df1[(df1['Number'] == #mobile number#)]
Chris = df1[(df1['Number'] == #mobile number#)]
ChrisM = df1[(df1['Number'] == #mobile number#)]
Austofix = df1[(df1['Number'] == #mobile number#)]
Simon = df1[(df1['Number'] == #mobile number#)]
Tony = df1[(df1['Number'] == #mobile number#)]
Trial = [Ewen, Alex, Nirmal, Chris, ChrisM, Austofix, Simon, Tony]
enter code heredf3 = pd.concat(Trial)
Фотография матрицы /DataFrames Goal
Пример данных:
df1:
Date Type Number Duration
03/10/18 National Mobile 8156665498 4.00
03/10/18 National Mobile 8156665499 27.00
03/10/18 National Mobile 8156665500 21.00
02/10/18 National Mobile 8156665501 47.00
02/10/18 National Mobile 45687823456 47.00
02/10/18 National Mobile 45687823457 35.00
02/10/18 National Mobile 45687823458 55.00
30/09/18 National Mobile 45687823459 1.00
30/09/18 National Mobile 45687823460 41.00
30/09/18 CallForward to VoiceMail 8156665507 1.00
30/09/18 National Mobile 8156665508 3.00
29/09/18 National Mobile 8156665509 16.00
29/09/18 National Mobile 8156665510 2.00
29/09/18 National Mobile 8156665511 3.00
29/09/18 National Mobile 8156665512 2.00
28/09/18 13nnnn 8156665513 14.00
28/09/18 National Mobile 8156665514 25.00
df2:
WNumber name
45687823456 Ewen
45687823457 alex
45687823458 nirmal
45687823459 chris
45687823460 chris m
EDIT: добавлены левые/правые клавиши в соответствии с данными образца.
Это работает? Использование pd.merge.
df1 = pd.DataFrame([['04123', '60mins'], ['04723', '30mins'], ['04568', '10mins']], columns=['Number', 'Duration'])
df2 = pd.DataFrame([['Bob', '04123'], ['James', '04723'], ['Someone', '04567']], columns=['Name', 'WNumber'])
df_result = pd.merge(df2, df1, left_on=['WNumber'], right_on=['Number'])
Это дает
Name WNumber Number Duration
0 Bob 04123 04123 60mins
1 James 04723 04723 30mins
Дубликат столбца можно отбросить.
Я сделал это ниже Antoine, и он выплевывает KeyError: "Number", который является одним из моих превосходных названий столбцов? Любые мысли друг?
xls = pd.ExcelFile("C:\Users...… MobileData.xlsx")
df1 = pd.read_excel(xls, 'RawData',0)
df2 = pd.read_excel(xls, 'WorkNumbers',0)
df_result = pd.merge(df2, df1, on=['Number'])
left_on
и right_on
с примером именования данных :)
df1
иdf2
? Не стесняйтесь использовать вымышленные числа и имена, это просто для того, чтобы составить представление об именах и структуре ваших столбцов.