Pandas/python and working with a column, in a dataframe, with a date
<p>I am currently working on a Python/Pandas data science project for fun. The data that I am looking at has a Date column where the date looks like the following: 2016-07-16. The data type is also an object. What I want to do is go through each date and pull data from across that row. Now, some rows may have the same date because two separate attacks occurred on that date. (I am looking at terrorism data.) What I currently have done is the following:</p>

<pre><code>dates = []
start = 0;
while start &lt; 300: 
    date = data.iat[start, 1]
    dates.append(date)
    start += 1
</code></pre>

<p>This will get me ALMOST what I want. However, I have two problems, the start variable is set to 0 but I cannot go to 365 since, like I said, each date may have multiple attacks. So one year may have like 400 attacks. Is there a way that I could end the data collection at 2016-12-31 or 2017-01-01 for example? Basically, is there a way to quickly determine the number of attacks, per year for year after year? Thank you for any help! </p>

<p>Oh I will say that I was trying something like:</p>

<pre><code>newDate = pd.to_datetime(startdate) + pd.DateOffset(days=1)
</code></pre>

<p>or </p>

<pre><code>data['Date']) + timedelta(days=1)
</code></pre>

<p>to add one to the date to end at the year. Not getting what I wanted plus, there could be more than one entry per day. </p>

<p>to explain further I could have something like this:</p>

<pre><code>Date            Deaths     Country 
2002-01-01         2         India 
2002-01-02         0         Pakistan
2001-01-02         1         France
</code></pre>

<p>The data has about 20,000 points and I need to find a way to stop it at the end of each year. That is my main issue. I cannot go to 365 because there may be multiple terrorist attacks on the same date around the world. </p>

<p>Another way of dealing with the problem is through a dictionary</p>

<pre><code># Get column with the dates 
dates = df.iloc[:,0].values
year_attacks = {}
for date in dates:
    # Get year from the date
    year=str(date).split('-')[0]
    # If year is already in the dictionary increase number of attacks by 1
    if year in year_attacks:
       year_attacks[year]=year_attacks[year]+1
    # Else create new key
    else:
       year_attacks[year]=1
</code></pre>

python date pandas