[英]Creating a pandas DataFrame from unstructured text
Sorry for the noob question but here it goes.抱歉这个菜鸟问题,但它就在这里。 I'm trying to analyse some Facebook messages.
我正在尝试分析一些 Facebook 消息。 So far I downloaded an html file, turned it into a neat list with BeautifulSoup and now I'm trying to create a dataframe out of it.
到目前为止,我下载了一个 html 文件,用 BeautifulSoup 将它变成了一个整洁的列表,现在我正在尝试从中创建一个 dataframe。
I'm looking at this resource: https://datatofish.com/list-to-dataframe/ but it's not working out.我正在查看此资源: https://datatofish.com/list-to-dataframe/但它没有成功。
Here's the format of what I have now:这是我现在拥有的格式:
list = ['2019-01-07 12:51 PM', 'name1', 'hi how are you', 'im at home', 'wanna come over?', '2019-01-07 01:02 PM', 'name2', 'hell yeah', '🐟', 'ill bring beer', '2019-01-07 01:06 PM', 'name1', 'awesome', 'and so on']
I tried a couple of different things but I'm starting to think I bit more than I can chew.我尝试了几种不同的方法,但我开始觉得我有点吃不消了。 I'm learning at the moment.
我现在正在学习。
Here's the output I'm hoping to get:这是我希望得到的 output:
index date time name message
0 2019-01-07 12:51 PM name1 hi how are you
1 2019-01-07 12:51 PM name1 im at home
2 2019-01-07 12:51 PM name1 wanna come over?
3 2019-01-07 12:56 PM name2 hell yeah
I tried iterating over the list and filling columns as it went along and hit a date, name or message.我尝试遍历列表并填充列,然后点击日期、名称或消息。
As I said, I'm learning, so rather than solutions it would be amazing if you could point me in the right direction to research into.正如我所说,我正在学习,所以如果你能指出我正确的研究方向,而不是解决方案,那就太棒了。 I'd be very grateful.
我将不胜感激。 Thanks!
谢谢!
Edit: I tried a couple of existing message parsers but they all stopped being supported in 2018 for some reason.编辑:我尝试了几个现有的消息解析器,但由于某种原因,它们都在 2018 年不再受支持。 They also all give me parse error messages.
他们也都给我解析错误信息。
It is a bit ugly, but it works.这有点难看,但它有效。 I'll gladly upvote a more elgant solution !
我很乐意支持一个更优雅的解决方案!
l = iter(['2019-01-07 12:51 PM', 'name1', 'hi how are you', 'im at home', 'wanna come over?', '2019-01-07 01:02 PM', 'name2', 'hell yeah', '🐟', 'ill bring beer', '2019-01-07 01:06 PM', 'name1', 'awesome', 'and so on'])
df = pd.DataFrame()
# get first element in list
x = next(l)
# if element is the last, catch the IterationError and stop
try:
while 1:
# try to convert element to datetime
datetime = pd.to_datetime(x, format="%Y-%m-%d %H:%M %p")
# if successful get next element as name
x = next(l)
name = x
# get next elements as messages while they do not match datetime format
x = next(l)
while 1:
try:
# if datetime conversion is successful break while
pd.to_datetime(x, format="%Y-%m-%d %H:%M %p");
break
except ValueError:
# else add message to dataframe
df = df.append([{"datetime":datetime,"name":name,"msg":x}])
x = next(l)
except StopIteration:
pass
df["date"] = df["datetime"].dt.date
df["time"] = df["datetime"].dt.time
print(df)
datetime msg name date time
0 2019-01-07 12:51:00 hi how are you name1 2019-01-07 12:51:00
0 2019-01-07 12:51:00 im at home name1 2019-01-07 12:51:00
0 2019-01-07 12:51:00 wanna come over? name1 2019-01-07 12:51:00
0 2019-01-07 01:02:00 hell yeah name2 2019-01-07 01:02:00
0 2019-01-07 01:02:00 🐟 name2 2019-01-07 01:02:00
0 2019-01-07 01:02:00 ill bring beer name2 2019-01-07 01:02:00
0 2019-01-07 01:06:00 awesome name1 2019-01-07 01:06:00
0 2019-01-07 01:06:00 and so on name1 2019-01-07 01:06:00
Using regular expressions and list comprehensions, the list content is extracted and transformed into a Pandas dataframe:使用正则表达式和列表推导,列表内容被提取并转换为 Pandas dataframe:
import pandas as pd
import re
datetime_regex = re.compile(r"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}\sPM")
name_regex = re.compile(r"name\d+")
cols = ["date",
"time",
"name",
"message"
]
l = ['2019-01-07 12:51 PM',
'name1',
'hi how are you',
'im at home',
'wanna come over?',
'2019-01-07 01:02 PM',
'name2',
'hell yeah',
'🐟',
'ill bring beer',
'2019-01-07 01:06 PM',
'name1',
'awesome',
'and so on'
]
tmp = ''.join(l)
datetimes = re.findall(datetime_regex, tmp)
dates = [datetime[:11] for datetime in datetimes]
times = [datetime[11:] for datetime in datetimes]
names = re.findall(name_regex, tmp)
messages = [line
for line in l
if not line.startswith(('2019', 'name1', 'name2'))
]
data = [[[dates[0], times[0], names[0], msg]
for msg in messages[:3]],
[[dates[1], times[1], names[1], messages[3]]],
[[dates[2], times[2], names[2], msg]
for msg in messages[4:]]
]
flatten = [item for sublist in data for item in sublist]
df = pd.DataFrame(flatten, columns=cols)
print(df)
Which returns:返回:
date time name message
0 2019-01-07 12:51 PM name1 hi how are you
1 2019-01-07 12:51 PM name1 im at home
2 2019-01-07 12:51 PM name1 wanna come over?
3 2019-01-07 01:02 PM name2 hell yeah
4 2019-01-07 01:06 PM name1 🐟
5 2019-01-07 01:06 PM name1 ill bring beer
6 2019-01-07 01:06 PM name1 awesome
7 2019-01-07 01:06 PM name1 and so on
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.