循环显示熊猫数据框中的日期

时间:2020-07-04 18:33:44

标签: python pandas

我无法在此数据上创建循环:

            TCT
03/02/2020  105
03/03/2020  68
03/16/2020  55
03/08/2020  37
03/10/2020  36

通过high=df['Date'].value_counts().to_frame('TCT').head(5)

我想查看每个日期是否在我的数据框中包含一些单词。要搜索我正在搜索的单词,如下所示:

word=['mum','old man','children','family]
sub_df.apply(lambda x : x.str.contains('|'.join(word))).any(1)]

其中sub_df的定义如下:

ref='03/02/2020'
sub_df=df[df['Date']==ref]

示例

Date               Tt
03/02/2020         indent code by 4 spaces ...
03/02/2020         backtick escapes
...
03/03/2020         add language identifier to highlight code
03/03/2020         create code fences with backticks ` or tildes ~...
...
03/06/2020         to make links (use https whenever possible)

如何在上述日期添加循环?

1 个答案:

答案 0 :(得分:1)

df.set_index('date_column')

df.loc[ref].query(f'column == {value}')

# or 

def is_substr(row, value):
  if value in row:
    return row
  else:
    return None

df.loc[ref]['column'].apply(is_substr, args=['sub_string'])

然后使用df.isna().sum()df.dropna()


df = pd.DataFrame({'date':['1/2/2020']*3, 'col':['blah_1', 'blah_2', 'n32']})

df.set_index('date', inplace=True)

df.loc['1/2/2020']['col'].apply(is_substr, args=['2'])

date
1/2/2020      None
1/2/2020    blah_2
1/2/2020       n32
Name: col, dtype: object