gpt4 book ai didi

python - 尝试应用 extractall 函数时返回 pandas "Cannot concat indices that do not have same number of levels"错误

转载 作者:行者123 更新时间:2023-11-28 18:15:45 26 4
gpt4 key购买 nike

我正在尝试制作一个电子邮件提取脚本来读取 Outlook 电子邮件并将信息提取到 pandas 数据框中。

我在尝试从邮件正文中提取日期时遇到错误:

#group messages by Subject so that individual emails are grouped in threads 
dfgroup = df.groupby('Subject')

#attempt to extract all the dates mentioned in the email body
temp = dfgroup['Message'].apply(lambda x: x.str.extractall(r'(?P<extract>(?P<month>(January|February|March|April|May|June|July|August|September|October|November|December))\s(?P<date>\d{2})\,\s(?P<year>\d{4})\s(?P<time>\d{1,2}\:\d{2}\s(PM|AM)))'))

但是,我收到以下错误:

File "C:\Users\tioxr\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py", line 573, in _make_concat_multiindex
raise AssertionError("Cannot concat indices that do"

AssertionError: Cannot concat indices that do not have the same number of levels

关于如何绕过它有什么想法吗?

最佳答案

Pandas 的 groupby 构建一个 DataFrame 列表,对每个 DataFrame 应用一个函数,并最终连接到单个输出 DataFrame。在您的情况下,最后一步失败,因为其中一个组返回一个空的 DataFrame。

试试这个:

list_df = []
for key, group in df.groupby('Subject'):
list_df.append(
group.Message.apply(some_func)
)

out_df = pd.concat(list_df)

关于python - 尝试应用 extractall 函数时返回 pandas "Cannot concat indices that do not have same number of levels"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48519236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com