我在更新组合来自其他数据帧的数据的列上的日期时间值时遇到问题。 第一个df包括汽车租赁公司的汽车数据:
df_cars = pd.DataFrame([['1708092730', 'MERC', 'GLK270', 'PLT01', '', ''],
['T170900224', 'MERC', 'C200', 'PLT02', '', ''],
['1708092733', 'BMW', 'Χ3', 'PLT03', '', ''],
['T170902781', 'BMW', 'X6', 'PLT04', '', ''],
['T171002176', 'VW', 'PASSAT', 'PLT05', '', ''],
['1708090817', 'FORD', 'MUSTANG', 'PLT06', '', '']],
columns=['RefNo', 'Brand', 'Model', 'Plate', 'Driver', 'Date'])
第二个df包含车库出站记录:
df_out = pd.DataFrame([['1708092733', 'John', '2018-1-2', 'MC', 250],
['T170900224', 'Phil', '2018-1-15', 'AMEX', 400],
['T170902781', 'Ann', '2018-1-22', 'VI', 400],
['1708092730', 'Ann', '2018-1-28', 'AMEX', 200],
['1708092733', 'Jeff', '2018-2-1', 'VI', 200]],
columns=['RefNo', 'Driver', 'Date', 'Payment', 'Deposit'])
并且第三个df包括车库入站记录:
df_in = pd.DataFrame([['1708092733', '2018-1-18', 165],
['T170902781', '2018-2-12', 600],
['1708092730', '2018-2-15', 150]],
columns=['RefNo', 'Date', 'Ammount'])
RefNo是车钥匙上的独特条形码。 我正在尝试更新df_cars列:
以下更新代码可以正常运行
df_OutIn = pd.concat([df_out, df_in]) # concatenate Garage out & in entries
df_OutIn['Date'] = pd.to_datetime(df_OutIn.Date) # OutIn df Date column to datetile format
df_OutIn = df_OutIn.sort_values(by='Date') # sort OutIn df by Date
df_OutIn = df_OutIn.drop_duplicates(['RefNo'], keep='last') # remove duplicates and keep last update per car
df_tmp = df_cars[['RefNo', 'Driver']].set_index('RefNo') # Update 'Driver' in df_cars
df_tmp.update(df_OutIn.set_index('RefNo'))
df_cars['Driver'] = df_tmp.values
df_tmp = df_cars[['RefNo', 'Date']].set_index('RefNo') # Update 'Date' in df_cars
df_tmp.update(df_OutIn.set_index('RefNo'))
df_cars['Date'] = df_tmp.values
df_cars['Date'] = pd.to_datetime(df_cars.Date)
df_cars预计会更新为
RefNo Brand Model Plate Driver Date
0 1708092730 MERC GLK270 PLT01 2018-02-15
1 T170900224 MERC C200 PLT02 Phil 2018-01-15
2 1708092733 BMW Χ3 PLT03 Jeff 2018-02-01
3 T170902781 BMW X6 PLT04 2018-02-12
4 T171002176 VW PASSAT PLT05 NaT
5 1708090817 FORD MUSTANG PLT06 NaT
当处理整个数据集(25-30k记录)时,我收到警告
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
有没有改进这部分代码的建议? 提前谢谢!
答案 0 :(得分:1)
df_cars = df_cars.merge(df_OutIn[["RefNo","Date","Driver"]],how="left",on="RefNo")
因为您的df_cars在Date和Driver中有一个空列,所以合并会为列名添加后缀。要解决此问题,只需从df_cars中删除这些空列。