我正在尝试构建一个python数据框以捕获初始数据集,然后在包含数据的csv文件在指定的文件夹位置可用时更新字段。我已经建立了初始表并将其保存到csv,但是当我重新打开表并尝试引入下一个文本文件数据时,每次运行代码时,我无意中都为新数据复制了标题。读入新文本文件并将数据基于通用列名追加到行的最佳方法是什么?
我当前的尝试是将新的文本文件构建到它们自己的数据库中,匹配初始导入中的列标题,然后将通用标题与起始数据集合并。但是,就像我说的那样,这些列只是保持重复,然后再添加_x,_y等。
初始数据框示例...
Line Name,Bearing,SOL X,SOL Y,EOL X,EOL Y,FSP 0.5m,FSP 1m,Length km,Length m,LSP 0.5m,LSP 1m,Date,Julian Day,Seq,Storage Db,Time Start,SOG Start,Fix Start,SOL Bearing,Line Length,SOL Easting,SOL Northing,Obs SOL X,Obs SOL Y,Time End,Fix End,SOG End,Planned EOL X,Planned EOL Y,Obs EOL X,Obs EOL Y
A901,38.67269998,568453.03,4343701.73,569156.01,4344580.05,250,125,1.125,1125,2500,1250,,,,,,,,,,,,,,,,,,,,
A902,38.67269998,568476.45,4343682.99,569179.43,4344561.31,250,125,1.125,1125,2500,1250,,,,,,,,,,,,,,,,,,,,
A903,38.67269998,568499.87,4343664.24,569202.85,4344542.56,250,125,1.125,1125,2500,1250,,,,,,,,,,,,,,,,,,,,
要合并的数据示例...
,Line Name,Date,Julian Day,Seq,Storage Db,Time Start,SOG Start,Fix Start,SOL Bearing,Line Length,SOL Easting,SOL Northing,Obs SOL X,Obs SOL Y,Time End,Fix End,SOG End,Planned EOL X,Planned EOL Y,Obs EOL X,Obs EOL Y
0,A901,9/26/2019,269,37,0037_JD269_X331 - 0001.db,09:29:54,2.73,12792,128.67,1000.0,587985.95,4380278.68,587811.22,4380427.4,09:43:51,15594,3.28,588766.68,4379653.81,588901.53,4379545.64
1,A902,9/26/2019,269,38,0038_JD269_M104 - 0001.db,11:38:24,3.69,98260,218.67,42875.0,591391.62,4383593.91,591626.99,4383892.87,17:40:02,25764,3.02,564600.29,4350120.19,568980.53,4355589.75
2,A903,9/29/2019,273,80,0080_JD273_M305 - 0001.db,00:50:53,3.64,27721,38.67,1125.0,576455.88,4351038.29,576365.15,4350932.06,01:03:26,30615,3.78,577158.86,4351916.61,577275.9,4352057.35
我尝试使用各种适用的规则组合进行合并,附加和串联,但是没有任何运气。有些错误,其他则重复了列...
df_proj = pd.concat([df_in,df_acc], axis=1,sort=False)
或
df_proj = pd.merge(df_in, df_acc, on='Line Name', how = 'right')
我试图找到这种类型的问题的例子,但运气不佳,我走错了路。也许我不应该一开始就将新文本文件视为自己的数据框。但任何帮助将不胜感激。谢谢!
编辑:
预期结果如下...
Line Name,Bearing,SOL X,SOL Y,EOL X,EOL Y,FSP 0.5m,FSP 1m,Length km,Length m,LSP 0.5m,LSP 1m,Date,Julian Day,Seq,Storage Db,Time Start,SOG Start,Fix Start,SOL Bearing,Line Length,SOL Easting,SOL Northing,Obs SOL X,Obs SOL Y,Time End,Fix End,SOG End,Planned EOL X,Planned EOL Y,Obs EOL X,Obs EOL Y
A901,38.67269998,568453.03,4343701.73,569156.01,4344580.05,250,125,1.125,1125,2500,1250,9/26/2019,269,37,0037_JD269_X331 - 0001.db,09:29:54,2.73,12792,128.67,1000.0,587985.95,4380278.68,587811.22,4380427.4,09:43:51,15594,3.28,588766.68,4379653.81,588901.53,4379545.64
A902,38.67269998,568476.45,4343682.99,569179.43,4344561.31,250,125,1.125,1125,2500,1250,9/26/2019,269,38,0038_JD269_M104 - 0001.db,11:38:24,3.69,98260,218.67,42875.0,591391.62,4383593.91,591626.99,4383892.87,17:40:02,25764,3.02,564600.29,4350120.19,568980.53,4355589.75
A903,38.67269998,568499.87,4343664.24,569202.85,4344542.56,250,125,1.125,1125,2500,1250,9/29/2019,273,80,0080_JD273_M305 - 0001.db,00:50:53,3.64,27721,38.67,1125.0,576455.88,4351038.29,576365.15,4350932.06,01:03:26,30615,3.78,577158.86,4351916.61,577275.9,4352057.35
答案 0 :(得分:1)
如果需要,可以用另一个DataFrame
替换缺少的值的解决方案:
df = (df_in.set_index('Line Name')
.combine_first(df_acc.set_index('Line Name'))
.reset_index()
.reindex(columns=df_in.columns))
print (df)
Line Name Bearing SOL X SOL Y EOL X EOL Y FSP 0.5m \
0 A901 38.6727 568453.03 4343701.73 569156.01 4344580.05 250
1 A902 38.6727 568476.45 4343682.99 569179.43 4344561.31 250
2 A903 38.6727 568499.87 4343664.24 569202.85 4344542.56 250
FSP 1m Length km Length m ... SOL Northing Obs SOL X Obs SOL Y \
0 125 1.125 1125 ... 4380278.68 587811.22 4380427.40
1 125 1.125 1125 ... 4383593.91 591626.99 4383892.87
2 125 1.125 1125 ... 4351038.29 576365.15 4350932.06
Time End Fix End SOG End Planned EOL X Planned EOL Y Obs EOL X \
0 09:43:51 15594.0 3.28 588766.68 4379653.81 588901.53
1 17:40:02 25764.0 3.02 564600.29 4350120.19 568980.53
2 01:03:26 30615.0 3.78 577158.86 4351916.61 577275.90
Obs EOL Y
0 4379545.64
1 4355589.75
2 4352057.35
[3 rows x 32 columns]
如果需要concat
与Line Name
一起使用,并且仅删除NaN
的列:
df_proj = (pd.concat([df_in.set_index('Line Name').dropna(how='all', axis=1),
df_acc.set_index('Line Name')], axis=1, sort=False)
.reset_index())
print (df_proj)
Line Name Bearing SOL X SOL Y EOL X EOL Y FSP 0.5m \
0 A901 38.6727 568453.03 4343701.73 569156.01 4344580.05 250
1 A902 38.6727 568476.45 4343682.99 569179.43 4344561.31 250
2 A903 38.6727 568499.87 4343664.24 569202.85 4344542.56 250
FSP 1m Length km Length m ... SOL Northing Obs SOL X Obs SOL Y \
0 125 1.125 1125 ... 4380278.68 587811.22 4380427.40
1 125 1.125 1125 ... 4383593.91 591626.99 4383892.87
2 125 1.125 1125 ... 4351038.29 576365.15 4350932.06
Time End Fix End SOG End Planned EOL X Planned EOL Y Obs EOL X \
0 09:43:51 15594 3.28 588766.68 4379653.81 588901.53
1 17:40:02 25764 3.02 564600.29 4350120.19 568980.53
2 01:03:26 30615 3.78 577158.86 4351916.61 577275.90
Obs EOL Y
0 4379545.64
1 4355589.75
2 4352057.35
[3 rows x 32 columns]
编辑:
对于重复检查:
print (df_in[df_in['Line Name'].duplicated(keep=False)])
print (df_acc[df_acc['Line Name'].duplicated(keep=False)])
要通过Line Name
删除重复项,请使用:
df_in = df_in.drop_duplicates('Line Name')
df_acc = df_acc.drop_duplicates('Line Name')
答案 1 :(得分:1)
让我们说您的起始数据帧为df_in
columns = df_in.columns
df_proj = pd.concat([df_in,df_acc[columns]], axis=0,sort=False)
答案 2 :(得分:0)
您可以执行以下操作:
#using append
final_df = df_in.append(df_acc)
使用axis=0
,表示row wise
df_proj = pd.concat([df_in,df_acc], axis=0,sort=False)