简体   繁体   English

如何从 pandas 数据帧中的逗号分隔值计算以特定 substring 开头的字符串的出现次数?

[英]How to count the occurrences of a string starts with a specific substring from comma separated values in a pandas data frame?

I am new to Python.我是 Python 的新手。 I am working with a dataframe (360000 rows and 2 columns) that looks something like this: business_id date我正在使用看起来像这样的 dataframe(360000 行和 2 列):business_id date

P01         2019-07-6 , 2018-06-05, 2019-07-06...
P02         2016-03-6 , 2019-04-10
P03         2019-01-02

The date column has dates separated by comma and dates from year 2010-2019.日期列包含用逗号分隔的日期和 2010-2019 年的日期。 I am trying to count only the dates for each month that are in year 2019 for each business id.我试图仅计算每个企业 ID 的 2019 年每个月的日期。 Specifically, I am looking for the output:具体来说,我正在寻找 output:

在此处输入图像描述

Can anyone please help me?谁能帮帮我吗? Thanks.谢谢。

You can do as follows您可以执行以下操作

  1. first use str.split to separate the dates in each cell to a list,首先使用str.split将每个单元格中的日期分隔到一个列表中,
  2. then explode to flatten the lists然后explode以展平列表
  3. convert to datetime with pd.to_datetime and extract the month使用pd.to_datetime转换为日期时间并提取月份
  4. finally use pd.crosstab to pivot/count the months and join.最后使用pd.crosstab来透视/计算月份并加入。

Altogether:共:

s = pd.to_datetime(df['date'].str.split('\s*,\s*').explode()).dt.to_period('M')

out = pd.crosstab(s.index,s )

# this gives the expected output
df.join(out)

Output ( out ): Output( out ):

date   2016-03  2018-06  2019-01  2019-04  2019-07
row_0                                             
0            0        1        0        0        2
1            1        0        0        1        0
2            0        0        1        0        0

If they are not datetime objects yet, you may want to start by converting the column (series) to datetime: pd.to_datetime() Note: the format parameter.如果它们还不是日期时间对象,您可能希望首先将列(系列)转换为日期时间: pd.to_datetime()注意: format参数。

Then you can access the datetime attributes through .dt然后您可以通过.dt访问日期时间属性

ie df[df.COLUMN_NAME.dt.month == 5]df[df.COLUMN_NAME.dt.month == 5]

暂无
暂无

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

相关问题 计算熊猫数据框中的出现次数 - Count occurrences in Pandas data frame 将 pandas 数据框列值转换为逗号分隔的字符串 - convert pandas data frame column values into comma separated strings 如何将 pandas 数据帧内的嵌套逗号分隔列转换为 Python 中的特定格式 - How to convert nested comma separated column inside a pandas data frame to specific format in Python 如何在 excel 的一个单元格中插入 pandas 数据帧(使用 openpyxl),其中的值将用逗号分隔? - How to insert a pandas data frame in one cell in excel (using openpyxl), where the values will be separated with comma? 如何使用 python 分隔 pandas 数据帧中的嵌套逗号分隔列值? - How to separate nested comma separated column values in pandas data frame using python? 如何查找存储在 pandas 数据框列中的逗号分隔字符串中唯一值的数量? - How to find the number of unique values in comma separated strings stored in an pandas data frame column? 计算pandas数据框中逐列的出现次数 - count occurrences of number by column in pandas data frame 熊猫-计算并从一列中获取唯一的字符串值 - Pandas - Count and get unique occurrences of string values from a column 如何从数据框(熊猫)中打印特定值(字符串)的数据 - How to print data for a specific value (string) from a data frame (pandas) 字符串中间特定 substring 后的逗号分隔数字 - Comma separated number after specific substring in middle of string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM