这是我的数据集
Month Date Time Log Command
Apr 4 20:30:33 200.200.200.254 dns,packet person: --- got query from 10.10.10.243:30648: Query
Apr 4 20:30:33 200.200.200.254 dns,packet person: id:78b1 rd:1 tc:0 aa:0 qr:0 ra:0 QUERY 'no error' Not Command
Apr 4 20:30:33 200.200.200.254 dns,packet person: question: home.twitter.com:a:IN Not Command
Apr 4 20:30:34 200.200.200.254 dns,packet person: --- sending udp query to 200.10.10.10:53 Sending
Apr 4 20:30:34 200.200.200.254 dns,packet person: id:99a1 rd:1 tc:0 aa:0 qr:0 ra:0 QUERY 'no error' Not Command
Apr 4 20:30:34 200.200.200.254 dns,packet person: question: home.twitter.com:a:IN Not Command
在此数据集中,我想每3行变成一行,但实际上我想使其一行,约束始终是3行变成1行,是的,命令是3行中的第一行,因为我需要出于机器学习的目的
低于预期结果:
Month Date Time Command IP1 IP2 user id url message
Apr 4 20:30:33 Query 200.200.200.254 10.10.10.243:30648 person 78b1 home.twitter.com no error
Apr 4 20:30:34 Sending 200.200.200.254 200.10.10.53 person 99a1 home.twitter.com no error
答案 0 :(得分:2)
我尝试将str.extract与正则表达式结合使用。希望我对您的数据没有太多的假设
df
Month Date Time Log Command
Apr 4 20:30:33 200.200.200.254 dns,packet person: --- got que... Query
Apr 4 20:30:33 200.200.200.254 dns,packet person: id:78b1 rd:... Not Command
Apr 4 20:30:33 200.200.200.254 dns,packet person: question: h... Not Command
Apr 4 20:30:34 200.200.200.254 dns,packet person: --- sending... Sending
Apr 4 20:30:34 200.200.200.254 dns,packet person: id:99a1 rd:... Not Command
Apr 4 20:30:34 200.200.200.254 dns,packet person: question: h... Not Command
data = df.reset_index()
data.columns = ["month_name"] + list(data.columns)[1:]
new_df = pd.DataFrame()
new_df = data[data.index % 3 == 0]
new_df['IP2'] = data[data.index % 3 == 0].Log.str.extract(r'(\d*.\d*.\d*.\d*:\d*)?:*$').values
new_df['IP1'] = data[data.index % 3 == 0].Log.str.extract(r'(\d*.\d*.\d*.\d*)\s').values
new_df['user'] = data[data.index % 3 == 1].Log.str.extract(r'(\w*):\s-*').values
new_df['id'] = data[data.index % 3 == 1].Log.str.extract(r'id:(\w*)\s').values
new_df['message'] = data[data.index % 3 == 1].Log.str.extract(r"'(\w*\s*\w*)'").values
new_df['url'] = data[data.index % 3 == 2].Log.str.extract(r'question:\s*(\w*.+):\w*:').values
new_df = new_df.drop(columns=["Log"]).set_index("month_name", drop=True)
new_df.columns.name = "Month"
new_df.index.name = None
new_df
Month Date Time Command IP2 IP1 user id message url
Apr 4 20:30:33 Query 10.10.10.243:30648 200.200.200.254 person 78b1 no error home.twitter.com
Apr 4 20:30:34 Sending 200.10.10.10:53 200.200.200.254 person 99a1 no error home.twitter.com