Python 根据列值映射行
如果日志文件是csv格式的,则可以在R/Python中使用Python 根据列值映射行,python,r,merge,Python,R,Merge,如果日志文件是csv格式的,则可以在R/Python中使用merge轻松完成此任务 但日志文件是用以下语法编写的 Key=1|Time=146656456446 Key=2|Time=146656456447 Key=1|Time=146656456448|field=10 Key=2|Time=146656456450|field=11 有什么方法可以合并它并通过以下方式获得差异 Key,Time1,Time2,diff,field Key=1,146656456446,1466564564
merge
轻松完成此任务
但日志文件是用以下语法编写的
Key=1|Time=146656456446
Key=2|Time=146656456447
Key=1|Time=146656456448|field=10
Key=2|Time=146656456450|field=11
有什么方法可以合并它并通过以下方式获得差异
Key,Time1,Time2,diff,field
Key=1,146656456446,146656456448,2,10
Key=2,146656456447,146656456450,3,11
如果您不需要列中的时间,那么下面的方法就可以了
library(tidyverse)
library(data.table)
df <- read_table(
"test
Key=1|Time=146656456446
Key=2|Time=146656456447
Key=1|Time=146656456448
Key=2|Time=146656456450" )
库(tidyverse)
库(数据表)
df%
分离(时间,分为=c(“时间点”,“时间”),sep=“=”)
df
#一个tibble:4×3
关键时间点时间
*
1键=1次146656446
2键=2次146656447
3键=1次146656448
4键=2次146656450
将时间更改为数字并按键分组以计算差异
df$Time <- as.numeric(df$Time)
df <-
df %>%
group_by(Key) %>%
summarise(Diff = diff(Time))
df
# A tibble: 2 × 2
Key Diff
<chr> <dbl>
1 Key=1 2
2 Key=2 3
df$Time%
总结(差异=差异(时间))
df
#一个tibble:2×2
键差
1键=1键
2键=2键3
将我的评论转换为答案,下面是一种使用“data.table”包的方法
为了读取日志文件,我做了以下假设:
- 您知道需要两列
- 您的日志文件当前没有列名(因此
)标题=FALSE
- 您希望数据由
字符分隔,而|
将能够自动检测到该字符fread
使现代化 虽然不漂亮,但它很管用
dcast(getanID(cSplit(mydt, names(mydt), "="), "Key_2"),
Key_2 ~ .id, fun=list(I, I), value.var = list("Field_2", "Time_2"), fill = 0)[
, c("Field_2_I_1", "Diff") := list(NULL, Time_2_I_2 - Time_2_I_1)][]
## Key_2 Field_2_I_2 Time_2_I_1 Time_2_I_2 Diff
## 1: 1 10 146656456446 146656456448 2
## 2: 2 11 146656456447 146656456450 3
样本数据
##只是为了模拟您描述的日志文件。。。。
##“temp”将是您的实际文件。。。。
你能更精确一点吗?您知道可能的键值的数量吗?如果任务在R或Python中很简单,那么什么会阻止您使用它们呢?键值用于映射到相应的时间戳(总是有一对,键值将是int)。如果格式使用列标题(如csv),任务很简单,我可以使用按键列合并。希望我是clear@pythonRcpp,从中读取数据,gsub
取出“Key=”和“Time=”,将数据重新格式化为“宽”格式,并为差异添加一列。此外,您的“diff”似乎是错误的。@A5C1D2H2I1M1N2O1R2T1更正:)很久没见到您了。。很高兴你回来了@阿克伦,谢谢。不知道我是如何“回来”的:-)这些天工作太多了!这对我来说最有效,只是每对中有一行有一列额外的内容。有没有办法将其包含到合并行中?例如,row1 Key=1 | Time=123 row2 Key=1 | Time=125 | extraColumnVal=99 mergedRow2:1123125,2,99
更新了我的问题,好像我忘了在question@pythonRcpp,为什么不多分享几行样本数据,这样就不会对正在处理的内容和想要得到的输出产生歧义。例如,是否仅为第二个键实例显示额外的列?
library(data.table)
x <- "path/to/yourLogFile.txt"
mydt <- fread(x, header = FALSE, col.names = c("Key", "Time"))
dcast(mydt[, Time := as.numeric(sub("Time=", "", Time))][
, Ind := sequence(.N), Key], Key ~ Ind, value.var = "Time")[
, Diff := `2` - `1`][]
# Key 1 2 Diff
# 1: Key=1 146656456446 146656456448 2
# 2: Key=2 146656456447 146656456450 3
library(splitstackshape)
dcast(getanID(cSplit(mydt, "Time", "="), "Key"),
Key ~ Time_1 + .id, value.var = "Time_2")[
, Diff := Time_2 - Time_1, by = Key][]
# Key Time_1 Time_2 Diff
# 1: Key=1 146656456446 146656456448 2
# 2: Key=2 146656456447 146656456450 3
dcast(getanID(cSplit(mydt, names(mydt), "="), "Key_2"),
Key_2 ~ .id, fun=list(I, I), value.var = list("Field_2", "Time_2"), fill = 0)[
, c("Field_2_I_1", "Diff") := list(NULL, Time_2_I_2 - Time_2_I_1)][]
## Key_2 Field_2_I_2 Time_2_I_1 Time_2_I_2 Diff
## 1: 1 10 146656456446 146656456448 2
## 2: 2 11 146656456447 146656456450 3
## Just to simulate a log file like the one you describe....
## "temp" would be your actual file....
x <- c("Key=1|Time=146656456446", "Key=2|Time=146656456447",
"Key=1|Time=146656456448|field=10", "Key=2|Time=146656456450|field=11")
temp <- tempfile()
writeLines(x, temp)
mydt <- fread(temp, header = FALSE, fill = TRUE,
col.names = c("Key", "Time", "Field"))
mydt
## Key Time Field
## 1: Key=1 Time=146656456446
## 2: Key=2 Time=146656456447
## 3: Key=1 Time=146656456448 field=10
## 4: Key=2 Time=146656456450 field=11