Trying to translate a python script into Julia and Julia seems more than up to it (admittedly I have a very primitive understanding of Julia so I expected some difficulty). In a nutshell, I'm trying to split a data frame by a column vector (that has 32 levels!!) and then write those partitioned data frames to text. In python, I was graciously advised to write something like this to split the data frames and store them in a dict:

injuries = {injury: df[df['Type'] == injury] for injury in df['Type'].unique()}

injuries['BROKEN PELVIS']

Does anyone out there know how to achieve something similar in Julia? I would imagine Julia is syntactically similar but all my efforts thus far have been fruitless. Any input is sincerely appreciated. Thanks. Chase CB

有帮助吗?

解决方案

Lets make up some data:

df = DataFrame(val=rand(8), injury = [rep("shoulder",4), rep("leg",4)])

This version does the same as the Python version:

injuries = [injury=>df[df[:injury] .== injury,:] for injury in unique(df[:injury])]

Now injuries is a dictionary of DataFrames, one DataFrame for each injury. You can then just do:

injuries["shoulder"]

and it returns a DataFrame with only the shoulder injuries.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top