How to copy a DataFrame structure in an empty DataFrame

2,019 views
Skip to first unread message

Fred

unread,
Mar 24, 2016, 12:35:09 PM3/24/16
to julia-users
Hi,

I have a DataFrame "df" and I would like to create a new DataFrame "m" with  the same colnames than df.  
And then, append to "m" the rows of "df" matching a complex set of conditions, so I need to test many conditions for each row. 

The problem I have is to create an empty dataframe "m" with the sames colnames of "df" and then to append the rows matching conditions to it.

df = readtable("file.tsv")

# trying to create an empty copy of df
m
= DataFrame()
names
!(m, names(df)) # produce error ArgumentError: Length of nms doesn't match length of x
# but I did not manage to create m with the same cols number than df.


z
= size(df)[1]

# for each row of df append the rows matching many conditions on many columns
   
for i = 1:z
     
if condition(df[:colname_x][i])
        push
!(m,i)
     
end
 
end

Thank you for your comments !

Milan Bouchet-Valat

unread,
Mar 24, 2016, 12:44:37 PM3/24/16
to julia...@googlegroups.com
You can use similar() for this:
m = similar(df, 0)


Regards

Fred

unread,
Mar 24, 2016, 2:05:31 PM3/24/16
to julia-users
Thank you very much Milan !
When I searched a solution in DataFrame documentation I did not found the keyword "similar". Is there an equivalent for Julia of this website for Perl for example ?

julia> df = readtable("test2.tsv")
4x4 DataFrames.DataFrame
Row row1   x3_39 x2_5 x4_5
┝━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━┿━━━━━━┥
1   "row2" 3.46   2.7   4.8  
2   "row3" 3.58   2.9   4.7  
3   "row4" 3.7   2.7   4.9  
4   "row5" 3.81   3.0   5.3  

julia
> m = similar(df,0)
0x4 DataFrames.DataFrame


julia
> m
0x4 DataFrames.DataFrame

julia
> df[2,:]
1x4 DataFrames.DataFrame
Row row1   x3_39 x2_5 x4_5
┝━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━┿━━━━━━┥
1   "row3" 3.58   2.9   4.7  

julia
> append!(m, df[2,:])
1x4 DataFrames.DataFrame
Row row1   x3_39 x2_5 x4_5
┝━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━┿━━━━━━┥
1   "row3" 3.58   2.9   4.7  

julia
> m
1x4 DataFrames.DataFrame
Row row1   x3_39 x2_5 x4_5
┝━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━┿━━━━━━┥
1   "row3" 3.58   2.9   4.7  



Milan Bouchet-Valat

unread,
Mar 24, 2016, 2:10:14 PM3/24/16
to julia...@googlegroups.com
Le jeudi 24 mars 2016 à 11:05 -0700, Fred a écrit :
> Thank you very much Milan !
> When I searched a solution in DataFrame documentation I did not found
> the keyword "similar". Is there an equivalent for Julia of this
> website for Perl for example ? 
Well, it's listed here in the brand new documentation site:
http://juliastats.github.io/DataFrames.jl/stable/lib/maintypes/

But maybe it's not easy to find...


Regards

Fred

unread,
Mar 24, 2016, 2:24:06 PM3/24/16
to julia-users
Thank you Milan for this useful link.
This website contains more informations than mine, but it does not seems to contains all the functions available for dataframes. For example I did not manage to find that I have to use append! instead of push! to add a row to a dataframe.

Tom Short

unread,
Mar 24, 2016, 2:28:44 PM3/24/16
to julia...@googlegroups.com
Feel free to file issues for missing documentation or (even better) pull requests.

Fred

unread,
Mar 25, 2016, 11:26:01 AM3/25/16
to julia-users
Thank you tshort. Is there a website or email address to post requests concerning documentation ?

Milan Bouchet-Valat

unread,
Mar 25, 2016, 11:53:06 AM3/25/16
to julia...@googlegroups.com
Le vendredi 25 mars 2016 à 08:26 -0700, Fred a écrit :
> Thank you tshort. Is there a website or email address to post
> requests concerning documentation ?
Use the GitHub project for that. Note you can make pull requests by
editing the documentation online too.


Regards
Reply all
Reply to author
Forward
0 new messages