繁体   English   中英

在 pandas dataframe 内循环日期

[英]Loop on dates within a pandas dataframe

我在此数据上创建循环时遇到问题:

            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)

我想看看我的 dataframe 中是否包含每个日期的某些单词。 要搜索我正在做的单词如下:

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)

我怎么能在上述日期包含一个循环?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM