使用datetime确定时间间隔内的最高频率事件

时间:2017-01-22 00:57:34

标签: python datetime pandas iteration

我的数据框包含大量有关犯罪的统计数据,包括犯罪的日期和时间以及类别。

0       5/13/2015 8:55           VEHICLE THEFT   
1       5/13/2015 8:41          OTHER OFFENSES   
2       5/13/2015 8:36          OTHER OFFENSES   
3       5/13/2015 8:30            NON-CRIMINAL   
4       5/13/2015 8:17          OTHER OFFENSES   
5       5/13/2015 8:16          OTHER OFFENSES   
6       5/13/2015 8:10           LARCENY/THEFT   
7       5/13/2015 8:00                BURGLARY   
8       5/13/2015 8:00          MISSING PERSON   
9       5/13/2015 8:00          OTHER OFFENSES   
10      5/13/2015 8:00                 ASSAULT 
---

因此,对于上面的示例,它只会打印:“其他违规行为。”

这是一个庞大的数据库,超过400,000行。

我需要编写一个函数,允许我输入任何给定的时间范围(使用from和to),然后确定哪个犯罪类别的频率最高。这就是我所拥有的,它不起作用:

import pandas as pd
import csv
import datetime
timeData = open("timeData.csv")
df = pd.read_csv('timeData.csv')

from datetime import timedelta, date
df['Dates'] = pd.to_datetime(df['Dates']) #this converts the values in the Dates column to datetime format

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

start_date = date(2015, 5, 1)
end_date = date(2015, 6, 2)
for daterange(start_date, end_date):
    df['Category'].value_counts() 

我想遍历日期列(列A)并仅选择适合我的日期范围的日期。对于我的日期范围内的日期,我想计算每个犯罪的实例数(B列)。完成此操作后,我想打印最频繁发生的犯罪。

建议?

2 个答案:

答案 0 :(得分:4)

首先,要使此效率更高,请将索引设置为日期。假设您的日期列为Date,犯罪类别为Crime

# make pd.Series with date as the index and crimes as the values
d1 = df.set_index('Date').Crime.sort_index()

# function that uses date slicing and uses values counts
def most_freq(start, end):
    return d1[start:end].value_counts().index[0]

# demo
most_freq('2015-05', '2015-05')

'OTHER_OFFENSES'

答案 1 :(得分:3)

如果您只想按日期对DF进行分组:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="mahboob.cmec">

    <uses-permission android:name="android.permission.INTERNET" />

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".menu" />
        <activity android:name=".Reference" />
        <activity android:name=".Registration" />
        <activity android:name=".RegistrationComplete" />
        <activity android:name=".electricitybill" />
        <activity android:name=".GasBill" />
        <activity android:name=".BillReport" />
        <activity android:name=".SetTarget" />
        <activity android:name=".EcoReport" />
        <activity android:name=".MapsActivity" />
        <activity android:name=".ManageContract" />
        <activity android:name=".GraphReport" />

        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps"></activity>
    </application>

</manifest>

或:

In [204]: df.groupby([pd.Grouper(key='date', freq='D')])['crime'].value_counts()
Out[204]:
date        crime
2015-05-13  OTHER OFFENSES    5
            ASSAULT           1
            BURGLARY          1
            LARCENY/THEFT     1
            MISSING PERSON    1
            NON-CRIMINAL      1
            VEHICLE THEFT     1
Name: crime, dtype: int64