문제

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