Python 搜索查询仅适用于数据帧的第一行

Python 搜索查询仅适用于数据帧的第一行,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个恼人的问题。我有一个包含两行的数据框架:第一行包含由tweet及其日期组成的元组,两者都是字符串数据(“text”、“date”)。我希望查询每一行是否存在特定的术语,并返回一个新的数据帧,其中只包含那些具有我想要的术语的tweet。我知道这两行都有多个条目带有相关术语。这是我的密码: data = pd.read_pickle('filepath.pkl') dict_twit = {k:[] for k in data.index} ## creates empty dict f

我有一个恼人的问题。我有一个包含两行的数据框架:第一行包含由tweet及其日期组成的元组,两者都是字符串数据(“text”、“date”)。我希望查询每一行是否存在特定的术语,并返回一个新的数据帧,其中只包含那些具有我想要的术语的tweet。我知道这两行都有多个条目带有相关术语。这是我的密码:

data = pd.read_pickle('filepath.pkl') 

dict_twit = {k:[] for k in data.index} ## creates empty dict for relevant tweets to go into

for i in data.index: ### data has a text-based index
    try:
        relevant_tweet = []
        for j in range(len(data.loc[i])):
            if 'query' in data.loc[i][j][0].lower():
                relevant_tweet.append(data.loc[i][j])
        dict_twit[i] = relevant_tweet
    except TypeError: ### The are empty cells in some rows
        dict_twit[i] = []

tweets_df = pd.DataFrame.from_dict(dict_twit, orient = 'index')
但是,当我运行代码时,只有第一行tweets_df有任何文本;第二行是空的。有人知道我做错了什么吗

编辑:以下是一些示例数据:

Index                Entries
digi_marketing_20th: ('RT @bigbomglobal: ? ? ?  Bigbom Interview with Dr. Long Vuong, Founder and CEO of Tomochain at MOU SIGNING CEREMONY ', '20/03/2018') , ('The latest ? eDGTL? News ?!  #digitalmarketing', '20/03/2018')
digi_marketing_21st: ('#DigitalMarketing See Top 3 Content creation tools Updated for 2017 ', '21/03/2018'), ('RT @sheerazhasan: Sheeraz, Inc digital marketing strategy for your business or brand! #digitalmarketing #socialmedia', '21/03/2018')

这是使用
collections.defaultdict
更有效的方法

出于性能原因,首选
df.itertuples
而不是
df.iterrows
,因为后者的开销很大

from collections import defaultdict
import pandas as pd

df = pd.DataFrame([['digi_marketing_20th:', ('RT @bigbomglobal: ? ? ?  Bigbom Interview with Dr. Long Vuong, Founder and CEO of Tomochain at MOU SIGNING CEREMONY ', '20/03/2018') , ('The latest ? eDGTL? News ?!  #digitalmarketing', '20/03/2018')],
                   ['digi_marketing_21st:', ('#DigitalMarketing See Top 3 Content creation tools Updated for 2017 ', '21/03/2018'), ('RT @sheerazhasan: Sheeraz, Inc digital marketing strategy for your business or brand! #digitalmarketing #socialmedia', '21/03/2018')]],
                  columns=['Index', 'Col1', 'Col2'])

#                   Index                                               Col1  \
# 0  digi_marketing_20th:  (RT @bigbomglobal: ? ? ?  Bigbom Interview wit...   
# 1  digi_marketing_21st:  (#DigitalMarketing See Top 3 Content creation ...   

d = defaultdict(list)

for idx, row in enumerate(df.itertuples()):
    for tweet, date in row[2:]:
        if 'digital' in tweet.lower():
            d[idx].append(tweet)

# defaultdict(list,
#             {0: ['The latest ? eDGTL? News ?!  #digitalmarketing'],
#              1: ['#DigitalMarketing See Top 3 Content creation tools Updated for 2017 ',
#               'RT @sheerazhasan: Sheeraz, Inc digital marketing strategy for your business or brand! #digitalmarketing #socialmedia']})

你能提供几行输入样本数据吗?当然,刚刚提供了,尽管数据很难看,也不容易形成这样的格式。要清楚的是,
Entries
列元组中的项是元组中的元组吗?目前,它们似乎不是有效的类型。似乎是
(('XXX','date1'),('YYY','date2'))
但是缺少外括号。对不起,这是我糟糕的格式。逗号分隔符也应该是列分隔符。因此col_1=('XXX','date1')col_2=('YYYY','date2')非常好,谢谢,非常好的解决方案!更整洁,特别是考虑到实际数据更大。谢谢!