在Python中找到次出现

2024-04-18 23:29:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个来自cvs的数据帧。你知道吗

我想知道在23:00到23:50之间,列“First”的行变为0的概率更大。你知道吗

                      Date First Second
0      2019-01-09 22:59:00     0     20
1      2019-01-09 23:04:00    14     32
2      2019-01-09 23:10:00     9     27
3      2019-01-09 23:11:00     7     27
4      2019-01-09 23:12:00     7     26
5      2019-01-09 23:13:00     7     26
6      2019-01-09 23:14:00     7     25
7      2019-01-09 23:15:00     6     25
8      2019-01-09 23:16:00     5     23
9      2019-01-09 23:17:00     4     22
10     2019-01-09 23:18:00     3     22
...                    ...   ...    ...
134761 2019-05-05 21:20:00    18     36
134762 2019-05-05 21:21:00    16     35
134763 2019-05-05 21:22:00    15     34
134764 2019-05-05 21:23:00    14     33

我使用此代码选择预期时间:

heure = df.set_index('Date').between_time('23:00:00','23:50:00')

但我不能抽出时间。你知道吗

如果您有任何建议:)

谢谢你

罗宾


Tags: 数据代码dfdateindextime时间between
3条回答

使用dt访问器怎么样?为您的用例更新了一个端到端的示例。你知道吗

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        'date': [
            pd.to_datetime('2019-01-09 22:59:00'),
            pd.to_datetime('2019-01-09 23:00:00'),
            pd.to_datetime('2019-01-09 23:49:59'),
            pd.to_datetime('2019-01-09 23:50:00'),
            pd.to_datetime('2019-01-09 23:51:00'),
        ],
        'value': [0, 0, 5, 6, 1]
    }        
)

# A mask to split the datset into two groups, based on the time.

df['in_range'] = np.where((df['date'].dt.hour == 23) & (df['date'].dt.minute < 50), 'In Range', 'Out of Range')

# A column that tests the condition you mentioned

df['condition'] = df['value'] == 0

# Group and get the average, which is the likelihood that value == 0, per group.

print(df.groupby('in_range')['condition'].mean())

提供:

                    mask
In Range        0.500000
Out of Range    0.333333

根据时间过滤。然后找出最常见的时间,其中First是0。你知道吗

try:
    (df.set_index('Date').between_time('23:00:00','23:50:00').reset_index()
       .loc[lambda x: x.First == 0].Date.dt.time.value_counts().index[0])
except IndexError:
    print('No matches')

这将返回一个datetime.time,或者对于示例数据,它将打印不匹配的内容,因为在指定的时间之间没有0。你知道吗

您应该首先将“Date”列数据转换为datetime类型,并且可以使用@smj提到的dt应用索引方法

import pandas as pd

df = pd.read_csv('./sample.csv')
df['Date'] = pd.to_datetime(df['Date'])
print df[(df['Date'].dt.hour == 23) & (df['Date'].dt.minute < 50)]

相关问题 更多 >