# Help with dataframes please

**URL:** https://discourse.julialang.org/t/help-with-dataframes-please/118502
**Category:** New to Julia
**Tags:** question, dataframes
**Created:** [August 22, 2024, 7:50pm UTC](https://discourse.julialang.org/t/help-with-dataframes-please/118502 "2024-08-22T19:50:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DTL](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@DTL](https://discourse.julialang.org/u/DTL)
#### Post date: [August 22, 2024, 7:50pm UTC](https://discourse.julialang.org/t/help-with-dataframes-please/118502/1 "2024-08-22T19:50:44Z")

</div>

Hi,

I’ve got a large CSV file I’m trying to analyse in Julia. I can load it into a data frame easily and I’ve got the column types identified correctly.

Format is basically

Location datetime value  
area1, 2024-07-20 20:30:05, 20.0  
…  
area2, 2024-07-20 20:30:07, 60.0  
…  
area3, 2024-07-20 20:30:30, 20.0  
…  
area4, 2024-07-20 20:30:22, 660.0

There are 4 areas, and the file contains a whole year of data per area, then moves onto the next area. This does mean there’s a lot of duplication in the datetime column. I don’t actually need accuracy to the level of seconds. This is just how the data is dumped from the network.

What I’d like to do it for each location, produce stats (min, max, mean) for each (Year, month, and week)

I don’t know where to start for this last part - I’d appreciate any help.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [August 22, 2024, 8:47pm UTC](https://discourse.julialang.org/t/help-with-dataframes-please/118502/2 "2024-08-22T20:47:58Z")

</div>

1. transform the `datetime` column into multiple columns holding year, month and week of year columns (using functions from the `Dates` module).
2. Then e.g. for yearly aggregates (assuming your data is in `df` data frame and you want yearly aggregates - for other this will be similar - and assuming you put year of observation in `:Year` column in step 1) do `combine(groupby(df, [:Location, :Year]), :value .=> [minimum, maximum, mean)`. (you need to load `Statistics` package for this to work).

---

<div class="post-metadata">

### Author: ![Andre\_G](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andre_g/32/211226_2.png) [@Andre\_G](https://discourse.julialang.org/u/Andre_G)
#### Post date: [August 23, 2024, 1:59am UTC](https://discourse.julialang.org/t/help-with-dataframes-please/118502/3 "2024-08-23T01:59:00Z")

</div>

Here are some tips for your question.

```julia
using CSV, DataFrames, Dates, Statistics

# Load the CSV file
df = CSV.read("your_file.csv", DataFrame)

# Convert datetime column to DateTime type
df.datetime = DateTime.(df.datetime, dateformat"yyyy-mm-dd HH:MM:SS")

# Create year, month, and week columns
df.year = year.(df.datetime)
df.month = month.(df.datetime)
df.week = week.(df.datetime)

# Group by location, year, month, and week, and calculate
grouped_df = combine(groupby(df, [:Location, :year, :month, :week]),
                     :value => minimum => :min_value,
                     :value => maximum => :max_value,
                     :value => mean => :mean_value)

# View the first few rows
first(grouped_df, 5)

```

---

<div class="post-metadata">

### Author: ![DTL](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@DTL](https://discourse.julialang.org/u/DTL)
#### Post date: [August 27, 2024, 8:36pm UTC](https://discourse.julialang.org/t/help-with-dataframes-please/118502/4 "2024-08-27T20:36:25Z")

</div>

Thank you! That was just what I needed.

---

<div class="post-metadata">

### Author: ![DTL](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@DTL](https://discourse.julialang.org/u/DTL)
#### Post date: [August 27, 2024, 8:36pm UTC](https://discourse.julialang.org/t/help-with-dataframes-please/118502/5 "2024-08-27T20:36:49Z")

</div>

Thank you very much.

---

<div class="post-metadata">

### Author: ![Andre\_G](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andre_g/32/211226_2.png) [@Andre\_G](https://discourse.julialang.org/u/Andre_G)
#### Post date: [August 29, 2024, 5:03am UTC](https://discourse.julialang.org/t/help-with-dataframes-please/118502/6 "2024-08-29T05:03:13Z")

</div>

Very nice! You’re welcome!
