Basic information about a data frame#

using DataFrames

Let’s start by creating a DataFrame object, x, so that we can learn how to get information on that data frame.

x = DataFrame(A=[1, 2], B=[1.0, missing], C=["a", "b"])
2×3 DataFrame
RowABC
Int64Float64?String
111.0a
22missingb

The standard size function works to get dimensions of the DataFrame,

size(x), size(x, 1), size(x, 2)
((2, 3), 2, 3)

as well as nrow and ncol from R.

nrow(x), ncol(x)
(2, 3)

describe gives basic summary statistics of data in your DataFrame (check out the help of describe for information on how to customize shown statistics).

describe(x)
3×7 DataFrame
Rowvariablemeanminmedianmaxnmissingeltype
SymbolUnion…AnyUnion…AnyInt64Type
1A1.511.520Int64
2B1.01.01.01.01Union{Missing, Float64}
3Cab0String

you can limit the columns shown by describe using cols keyword argument

describe(x, cols=1:2)
2×7 DataFrame
Rowvariablemeanminmedianmaxnmissingeltype
SymbolFloat64RealFloat64RealInt64Type
1A1.511.520Int64
2B1.01.01.01.01Union{Missing, Float64}

names will return the names of all columns as strings

names(x)
3-element Vector{String}:
 "A"
 "B"
 "C"

you can also get column names with a given element type (eltype):

names(x, String)
1-element Vector{String}:
 "C"

use propertynames to get a vector of Symbols:

propertynames(x)
3-element Vector{Symbol}:
 :A
 :B
 :C

eltype on eachcol(x) returns element types of columns:

eltype.(eachcol(x))
3-element Vector{Type}:
 Int64
 Union{Missing, Float64}
 String

Here we create some large DataFrame

y = DataFrame(rand(1:10, 1000, 10), :auto)
1000×10 DataFrame
975 rows omitted
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1106441028173
265108938355
335210115178
44693749264
59742314991
686410378815
73761649966
85477157224
98891981277
1066681910631
1199101764294
1214489105396
133366338921
9893535814595
99051053124438
9914818827848
992135710410716
99310277982373
9947392473161
995551057711510
996226771029510
99791758995710
99829894817101
999101057857477
10003132856751

and then we can use first to peek into its first few rows

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1106441028173
265108938355
335210115178
44693749264
59742314991

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
129894817101
2101057857477
33132856751

Using first and last without number of rows will return a first/last DataFrameRow in the DataFrame

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1106441028173
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
10003132856751

Displaying large data frames#

Create a wide and tall data frame:

df = DataFrame(rand(100, 100), :auto)
100×100 DataFrame
75 rows omitted
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20x21x22x23x24x25x26x27x28x29x30x31x32x33x34x35x36x37x38x39x40x41x42x43x44x45x46x47x48x49x50x51x52x53x54x55x56x57x58x59x60x61x62x63x64x65x66x67x68x69x70x71x72x73x74x75x76x77x78x79x80x81x82x83x84x85x86x87x88x89x90x91x92x93x94x95x96x97x98x99x100
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.1316470.7337650.7958190.7855690.7744370.84780.9497070.2410360.3521080.559870.1137940.1165540.8103110.8719960.05852150.9597790.9672530.7570610.1024350.5062850.879260.895220.901380.7909810.8034040.6080820.7419520.8888410.1816160.773630.2712530.5261540.8667770.4412410.5360.479470.7559330.4892230.2674190.5115810.05932440.9568510.1741020.8816810.1270880.8936220.2633260.992870.6864060.9791680.1359830.7482170.003960860.4596350.5484860.4879770.6060850.809630.7181390.6129210.6526560.9983750.8088660.2739220.1763570.9801280.6797590.4923660.007980820.1291720.8143250.150770.5559070.5123030.9471140.3212670.7307780.3865640.7000410.7208310.9986470.7983590.9835880.2367040.01939660.8746030.7051190.3656590.4986980.23360.959250.9839580.4310860.8179540.6520910.7819590.09159170.6719610.1299370.616734
20.002460220.6968210.1425250.5680060.1637270.05367560.8410880.9321540.4398450.7684490.647760.5210660.816430.8172480.2337880.4523480.01426780.1022630.01828280.3675130.1902930.1571660.4493160.4294660.9443830.7999650.9659780.9841960.9301580.08468360.7394330.8609890.6350960.7522680.7861010.736060.3700780.5373170.1024070.3597590.005305620.6010470.3534880.4806960.1214220.6970540.7153120.5108710.4634550.5576240.5804820.9985660.3239030.2281560.5119330.4771980.7006870.1586790.3282620.3910950.03900280.3752380.3663150.5366350.4978730.4493390.5008530.4792240.5491150.3280110.7754510.535440.703150.5309050.04677830.6988510.3487470.6518440.03646280.7074490.3822640.9424620.05391960.8808470.390060.8496290.3550060.8177030.0243640.388930.8694540.233210.1531940.5528380.4005180.05241650.5095330.7482530.4337680.775943
30.1235430.5508650.4210670.2221620.1074890.4956630.07778760.5522070.09843960.5288790.1515730.8319590.493430.2550170.2332990.1200870.09152640.6717680.6991110.6890250.3274740.7095430.916180.08961770.7502590.1664740.002449690.3111820.4631990.2853160.8711470.7089730.4298090.6089340.6203830.4254480.9538080.8931240.6357880.4680220.1380610.5040220.4755820.6122720.7065510.3887480.02483190.4502580.1962610.4405160.7875860.9195970.7377450.2388090.9369340.4843530.7217520.9312440.407950.987080.5151020.8669270.5399850.7044490.7513760.9218610.668390.3210630.8266030.8725740.8888660.03073120.6230540.06243510.1117530.1085580.693360.3607220.4680170.483410.924220.1562680.8388570.8820940.7294180.7928610.3264750.005368260.8436710.03429060.1859630.4979490.6604320.09051470.3198140.140930.8107740.5859830.878290.897587
40.5283950.7377660.0891490.3897760.1230860.149730.3583610.2941170.7887660.2414210.02616380.22720.8723540.8771140.3334960.020910.1166320.4890560.5939530.4426980.1261080.742180.4890920.0391820.03342030.994380.23940.5970960.4086740.4701010.919420.3668830.1943240.5414780.5998260.1629860.3260450.600790.2389860.5503440.5090760.5569890.5799910.871610.7746190.1296780.3412530.6280220.9837790.9502980.8532420.172180.777240.6228290.5593930.5859040.05973550.2383390.669210.3516530.3820030.5632810.6591750.6414120.8699430.5475220.7433580.3234640.2935710.6015360.2042290.769240.111180.2006650.6088790.2615430.7759430.8286450.1943980.2898820.255310.6479210.1841040.5304250.6188980.06983080.2200630.06330170.7202640.1323240.7295030.9167030.008528680.5235210.09322460.09269990.4120930.4772930.3653370.454724
50.6541290.3075720.9111990.3671890.4908850.6524690.6548510.5935370.5739230.6625550.9721610.8907460.9604160.6730550.02021790.4107350.004822010.2077190.1242570.1297790.1749790.5908660.2108710.8019520.64390.1238390.7506570.6459440.04313840.8902830.2840790.5152550.35320.6943870.9457460.8066290.3921590.6083520.2146140.9511870.487850.3363010.5930650.346860.5400160.600080.6350.1827660.6398550.338480.2946410.9634470.9825110.7531980.1372630.05456040.390540.8110740.6178240.4063170.1254660.0405510.9978760.4800030.2122540.8228230.807110.2636680.02776340.8517030.7566660.6168460.1926960.7155710.4019920.2216510.5518860.8013890.5034880.9745480.8551750.3470780.7424430.6604590.5363740.7998150.5520250.481040.2179330.1855820.8782680.6476270.02155650.4009840.1795840.6448550.5338260.9325370.6607930.253966
60.119490.3661690.4702080.02811960.1495380.09926280.9332520.3558020.2564430.0952460.6457190.09736740.8535880.9093260.5206370.006037020.962510.1724560.9957510.6576360.1022430.8928040.6716440.5667820.08765370.5992760.9947270.2795070.596590.2813560.2189040.1978390.6398720.4003290.7914380.1655960.4744020.03475190.9276450.3592350.5444460.3820650.001901580.009913220.2026150.213870.3968610.1533310.4687230.5638430.1390220.3943440.6960770.6684410.2783380.4374130.5295130.8901440.5762820.136360.8547230.4357940.007662440.6078140.2456080.5450950.05857570.09518910.6467830.8642730.1081010.1675690.118240.5277570.8308760.3575910.4599230.9200850.5590620.5568630.3930710.4822740.7900890.4867240.02165490.7632960.3673860.7944530.1459460.9775940.8863710.2005020.3790230.7524170.5323680.8516740.2661050.1677960.6016730.0961466
70.5028050.02566510.6460440.087140.2639730.4228980.486410.9547780.5195380.8776780.1378610.01319020.9417660.71280.9568570.2750750.3955160.3365890.3179690.4385660.7944920.1833070.2988320.2485820.5385870.863030.524890.9752180.6833460.9701560.499650.309610.2371810.727120.2155360.9623350.07279110.8345690.1424320.1631840.1435720.8207750.5875840.02125910.2354860.2247480.4508660.3795040.7312680.3290360.5372570.9407730.8416440.1839980.8793730.1964530.05242780.7956770.3848010.8610750.707280.2538910.4215030.3065650.6227960.6835630.3945780.9416410.4973970.5343670.1447840.2003450.6140130.7733740.3113440.7215550.07588740.4309810.7596190.7224960.1614370.5605820.4653210.3063240.02907570.1562420.9341040.07993730.1254170.9181950.5435890.8128210.6420420.1026540.3782610.3885390.6613410.02358020.6400070.299132
80.9089080.22450.8481920.5919240.1149630.8924650.002651510.4488980.5873850.8386690.2124580.4288340.4506130.7706530.2312480.4469220.6267490.2999620.1448330.3909750.08228110.698340.3400020.7288490.4670970.1353480.4144030.8442550.1900480.9603750.1027740.5382660.6418110.5787750.7908050.6285680.9196630.1078430.58260.07760240.03999990.3867180.8957380.4597550.1368660.52840.9253040.1868030.8006840.6548270.4829990.2414410.7459880.557770.3312950.6907180.7398340.01723960.8772980.3179370.1802410.46330.9335280.2613480.1957420.8660610.8126220.461420.1873930.4756020.4064410.2308150.1897230.4233730.5725190.07596510.5884770.4684720.8645850.6833510.1080740.165490.1507860.7812280.1308970.7664150.2338230.8588010.5822730.4550110.05720180.5658060.413560.2494750.8148560.663480.1734470.3690420.9886060.210147
90.7968660.9346830.6382990.8172270.3832660.2346390.2057980.9576160.8003360.610660.6697460.6397580.5352560.9924140.5572990.3372350.3510740.6012540.8086060.2350320.3265220.2965420.4942950.09139730.3101370.6160340.04159190.1529840.4261680.903550.6326170.1820010.005682980.3832210.7667490.1569220.02739910.2548760.06771120.02120960.604660.3452740.1924930.8111570.7102140.3279360.6144330.05621250.023910.1396470.6541840.5570780.4472020.6776050.1474620.8071870.9149180.4517060.388290.3855650.9530480.4335830.1098630.1684840.8506980.8495670.1341070.1753080.4592470.4537790.668070.5419880.5630550.8018530.5164090.7601540.5987160.09657510.7993750.4300360.4112290.5850480.7518840.3575040.8017650.7908550.2048760.2000280.569380.3244340.2635680.9357960.1382520.5068480.4888220.8356920.6976080.05740970.01971820.865277
100.02620370.4685450.5638370.8273910.1519540.3055930.9562680.3266130.4312510.6398240.4773530.550090.3179620.391880.424710.3175230.4865050.1369880.4515050.5860810.5183420.23810.3355950.4996330.9950240.7818120.303860.765530.2239130.08874810.1082440.9812360.3588360.5435710.6796580.03866150.9351530.6838220.6159720.05762010.7125570.2206130.3883910.4186310.3003890.02258630.09556530.4386490.4478060.8597030.5523640.2175030.8398620.2971730.1354630.3650020.8313470.9980830.5078430.7414330.7690010.5939350.9404820.8465330.7160880.0414850.6458790.4191760.517540.99220.06665930.7280830.2692080.8355460.5654530.3395520.04984590.7601510.3132470.9621690.5574280.8294640.9166830.4059090.9941180.974190.9577150.187870.4771340.07673250.6703280.7432460.9794940.1712780.218970.3765910.4063840.1393910.5137740.322011
110.02329010.629290.3492890.04369150.9307910.7899140.6343330.1937250.4978760.6537290.4023940.675160.6437290.7137780.3360310.9524120.005684550.7968980.1815070.2607620.8273950.6655820.2193090.7894930.5080960.7245290.7537880.05628660.5305690.3577090.3890140.9201680.6124320.9590610.8320620.2474110.6238770.2814720.08873050.07424630.2960990.8548770.3085160.9358870.5576370.2819860.07848860.388260.05409180.5244850.05352520.6093320.8444710.8473110.5566830.9514630.567790.6278620.5440370.608070.8471540.1984590.9845430.1377850.9194320.4250560.8229570.09102830.09001380.3897320.5749550.02239420.8869820.3941170.5168340.7975620.09998010.7290850.3906390.8792780.7557150.3099450.9142790.7644790.7231610.7636820.3927020.04289120.4387560.190980.3566380.1658410.1467860.5384040.03838130.2474980.4192760.640920.4439560.903002
120.4319670.9349970.7189750.1871920.6910990.387040.102550.1779030.2119880.1724310.3872420.6097566.94966e-60.2873670.4148550.991570.5879450.8020230.4142960.4359260.6576030.8021670.9098130.6339240.545320.2969320.8222620.09755970.2147840.5124680.6988510.9303280.9401070.5170970.225670.2704290.01029020.04844470.5058880.3593050.5960310.1711780.5919450.3980280.05594480.4143140.6138070.1464040.1653550.1706920.3733540.8601620.2643530.5882180.08227880.07965530.3054950.1748830.1906550.5865880.1745650.3576230.2585950.1972020.4718450.2276390.635510.07532540.6737710.2512760.1487330.8578770.1607710.6910870.2381770.6363470.1388230.6241550.9038410.2964150.2943260.1654680.004869510.7238790.3986350.3860620.4847190.3797860.5212990.8853650.4807770.2969590.2737280.840080.7308410.06925860.1943890.8802050.1122970.398017
130.7613290.9457760.1274440.239670.3884060.2889440.5351540.350540.7902280.1467830.693110.1084810.430410.8536520.005244420.06395670.403030.1128840.4718280.8116450.3264410.376510.085740.9661640.9902160.9410330.07482210.8416170.488940.915930.4753570.1893320.4092450.8387760.24020.7648090.527790.3721480.5081570.6256520.7531090.007988170.3100130.9915530.6583160.6300520.9121850.4674240.8830860.3806260.7581210.8581280.07188320.1696120.8679570.5589660.9869850.338270.5766660.7690630.4941020.1912120.9796920.591490.9519590.7931940.9337130.6807780.5974760.2962180.9464080.5880340.1497370.05261670.2856690.9385020.6164960.8476390.01222140.1780010.2139230.1647460.9112050.5956350.9258840.8929580.366660.72430.9642920.714810.2878430.7592280.7408420.1320220.6697640.4437890.672070.305340.9876090.718269
890.4779550.8471350.6274940.9378330.05010110.1233550.4449940.8566420.2971260.01027730.1624560.8686760.6766560.6088160.7358150.6467820.654150.5987270.05700320.9317420.4569970.001519230.278350.1163680.572190.1956550.7483120.1295230.6779390.6232170.4221230.4432640.1413760.3175760.4581670.3945330.7713310.985920.5449750.4141250.1730640.2025720.09605090.1103830.08783510.7976980.3242330.5273120.9719420.2612550.977280.5349650.8079170.2931670.479960.6797390.5411420.6601650.5658570.1313740.06991580.2067510.6229410.3202620.4026130.6807130.9945120.7221450.5401230.7025940.8534890.6905340.3662870.02782430.03431750.6686830.6290420.06052340.7195770.6860990.8918220.5808810.08144310.2703640.6053910.5540710.5107950.6015780.8671070.5193520.3557190.9005960.3238080.4367690.85880.420880.169140.1826530.7052110.213207
900.8648690.5415150.7909960.2908940.5276490.8291530.0199370.7903480.9086730.1199240.5667680.6481580.8083580.05794130.944290.7968190.9498050.1660790.8164550.9091390.7861780.1323130.9929780.6300650.4421340.6942360.5211190.6199650.5853920.2038420.5512850.8915010.05046640.2444860.771810.3337520.184250.7645290.1138530.828240.9915840.7241510.966350.8419620.4021450.2988050.3980280.5156250.02488670.448230.9917450.8704950.8916590.9330210.4289760.6947620.07905980.6061140.1742930.8214080.5117220.7839420.5494530.4254460.4963530.5841230.2383630.3213920.5081320.1459710.58510.0382510.1074430.6212240.3980850.8082590.3322220.7981840.2614670.4861540.4215970.02909270.2023090.8820610.1157410.3117340.9278990.8597930.1603480.4918790.7840360.6991340.5516250.5754530.6256490.2967850.9128260.4125750.04752660.440747
910.4207790.05974830.358540.3731810.007170780.195750.6156290.08383320.1368570.05792260.6596390.623850.7470170.8480490.4540550.1494690.7373940.05583110.7089360.6722440.9476850.2979050.06950580.4213040.9502550.4044420.9290510.876320.7942320.03311660.6783750.1857630.5222560.05766960.2654290.06504420.7335670.5059610.5671410.4511520.8780250.3215090.6972990.8150180.1433150.8533240.4162190.3055690.6164730.5664130.5765820.04717080.2296160.01514620.4125340.39260.8367360.8699490.6889480.9168590.5830590.9556920.6549620.9466550.8955550.7395530.216510.1329580.7348480.3707150.1916640.1810940.6504570.7703690.2445050.4697160.1112660.0686290.8812970.8839540.1910650.5108690.03068580.7282730.863160.8343420.2260420.6727910.2135650.5886460.5061210.9409070.3876920.3537060.2794370.9122010.6384150.7252430.4737310.752345
920.3871640.4472330.8765230.7597390.514660.9901150.215210.228450.2082890.6573410.2274580.9430050.5884650.7202860.7021120.6797650.6175830.3215750.9149220.5713080.9208330.3235880.6274850.1680040.8987990.4593920.9304870.9030680.4085260.4978710.5889360.4319410.2191570.8325720.7877270.265030.2057450.9844910.7175050.3588890.4642610.1115470.4907750.3745760.4486320.1221070.8322630.7591230.5041230.4431420.597820.817630.2991960.05201380.4526670.08451720.2120950.464160.7448930.3167530.5083490.5904070.4335030.351430.6363660.977270.7154490.8321840.7580980.9293110.3335320.09463380.331040.2048770.493190.5326230.3018430.1672850.4482780.4895670.3424390.6924190.6949350.0243130.7902940.2384450.7986960.5991060.3400450.6608930.1162910.7062920.1596210.6638320.7225690.9910080.5884260.4562430.3072950.474447
930.617430.6589270.8642660.5510480.3089110.5523790.8421160.1346660.5412730.7891770.4422720.9814340.9668890.316660.08022040.8357080.6863680.9825980.5418830.5294410.0450840.4923320.1632220.08766630.6599940.6822840.3714840.7205870.1526330.04634980.7906180.7659450.7062080.3006080.4600660.462430.1307770.2885460.6986250.9073420.2080240.1387020.2152180.5351120.6883640.6787260.4713040.003226980.8559230.8678630.532640.7246940.7688220.8056040.4957770.6578540.3001230.7516460.3728720.8724560.6619260.7077720.2755240.37790.9374310.3097580.0008926350.5560990.4804740.1388630.3307510.9753480.6259760.455240.6529920.7825090.9820940.2940840.06932560.2819590.4254510.3408950.03065860.4803150.9356920.8540390.6177460.8204750.7759820.1669640.7005450.6611430.9539240.8489310.7713430.6186820.05045510.9561910.4164910.424735
940.1318060.9986730.2090930.959180.3316010.4158650.2657680.2085360.3301390.4653770.3381410.6488780.7858450.5887530.8492520.8848690.5107610.2850390.1469560.693230.2251210.7239040.188660.6085760.7025980.3880870.889680.9361670.01171250.4620170.6674130.1503920.07853680.6631130.1616480.6696820.05624910.2503430.6388760.4152960.8650440.8510420.8692180.7326120.8854440.3647650.08079720.04220840.5866550.2116860.5919030.8014080.3644610.2537330.2909660.6973540.531670.009991110.044760.3947930.7277090.431710.723560.0111190.5435370.3773540.1655660.3854130.5559330.01175480.6138050.9950630.7508120.02697740.6609830.2144790.850460.8751190.2395840.8263120.2424450.7698830.813490.3044680.4432910.1299120.5307680.153040.9425120.8635740.9645390.4595030.566580.8460380.1786180.1850960.9457490.5734160.9207510.77587
950.6589530.9903140.7101360.3352120.4230870.8807660.3187520.6701740.1746880.9522130.9227790.08325940.2642380.3894140.1634360.7383230.2149140.7895470.3550880.05452550.7937860.7194080.02214540.8781460.7120910.8229270.6107640.01592160.9978820.1852790.3115070.1621460.5075490.9713080.8774170.08025770.7781440.1057350.4098030.2909760.784410.1440480.4634810.7983790.3170650.2496920.7221250.09248810.5104360.346230.78220.7263040.9567180.2260150.1623910.2268850.5835360.9121370.03551430.7381280.1604320.9563690.8925470.08968440.400520.2472060.2306820.5835120.9144420.8749360.4190870.8651930.8593690.8844750.07733020.08797660.2572250.3004350.1977220.8364430.9307540.2034450.6338840.9724470.8429670.4846540.9120260.8912370.1830890.3187090.05478090.1439680.7353730.6727860.9780260.3798240.6557490.4323360.3660560.204915
960.8955760.8770250.9694050.2071480.1732150.6712660.8564720.9216630.6460770.5377240.2918590.0706830.05433240.04703520.229180.4434420.622160.05423470.8177190.4578210.5660950.7805470.3660890.755730.4767580.7515790.2886770.2428980.217460.2415910.09032580.564340.7970260.3582640.8154240.1784320.02065830.009511410.9151130.3924860.08517180.8462280.4040130.8486450.2718020.8238390.6100240.2704460.5868410.3717210.1423920.303390.3882340.9506340.1655180.09691690.134470.0130690.4888940.9407390.8022150.3798090.6905350.101320.8024460.129380.01944690.1963860.5430270.7278310.650560.234750.7460060.7528390.5403690.2657610.5766570.4426490.069590.08295220.7241080.4881810.5141350.4972510.0320050.7364450.5470860.3806830.03540550.19640.484220.3266320.596780.1544680.4653670.9190820.2624590.4797630.7745390.739952
970.4754490.3279170.9278020.4260140.1103110.6942030.6518520.01988520.2376390.8746630.7005110.2822910.809740.7534230.1701960.8893770.7499830.1981380.4497920.09542730.08749850.6919520.4031310.4503540.07933980.7306680.9539150.8340450.1145110.02283240.04949930.9661140.8955550.5673670.2099770.4558080.334660.56850.805060.768660.9512220.4841660.978540.7403670.7913640.4473120.4513880.4868420.2188440.5764680.7938140.3105870.9537940.05246060.1976340.9685050.2901270.7669580.4819630.9243970.09348440.2142230.3464260.3714930.2623380.6109550.3094390.184030.9381740.8602140.1094520.01469110.2974580.4496910.955960.1427290.2118150.6297890.9323420.5114040.8567840.3074980.0003895160.226810.4494450.1567410.2939180.2255960.28570.3733730.7729160.001309870.8001510.3232170.2588380.2651830.9085380.1803340.5806190.336444
980.3188610.8540780.2641860.9732130.6733520.8503170.8246990.3705760.5319370.8662310.2639520.9303220.09485090.5357330.1081260.9629760.5059940.3995950.08072380.3267080.04361260.7354280.6150020.8389430.1347830.5186820.05353110.5294910.567990.4143670.6882110.5176850.4157830.8705950.07126610.6835740.5897490.3310480.1132950.678850.2340040.1376870.95560.8695710.24180.2956580.8366860.8642870.1474880.7476550.0902150.9131860.8065710.9950180.6306040.05075570.2426540.5411090.7806340.5125520.1292730.2731360.1659410.1244910.5757160.9280530.04486190.1451940.6676330.3436770.2644210.4262370.7602330.8126830.5048320.9765720.7735860.8217450.8938530.1603880.7917060.5137570.7369510.8244060.1774060.2177820.7635580.2770750.1486330.9286650.6355430.1590130.01648920.003810410.966670.9482930.9222730.3201840.4854470.52224
990.3240530.07511080.3117350.1491370.3904890.9386840.05162990.02898030.329990.4604070.4942550.7182580.1240420.07506170.8164110.3650440.8569570.5305480.9195760.07999290.520810.3857660.2287680.4966620.9325320.7983990.9817280.125180.6426330.7053690.3867220.5511410.4056340.3747440.9028020.1768610.1151760.8799170.3879980.6943640.2659280.8610620.5560670.009858460.9309340.0823680.7299270.3349870.07705620.547250.4822010.2835860.2869780.304910.3952610.4499280.5522780.6514780.1750260.06441530.4365630.2606840.5367510.8553050.2711650.9779810.4942840.3754130.4473090.3816680.7623110.7043950.3814970.467170.005151720.3806720.5614050.2048260.3643160.9507330.7181350.5907310.7180730.9131480.8911350.03677180.2714210.7948250.2702210.9595310.2576240.3013010.3352740.3899050.02712980.2717190.2849140.07032260.4910740.216349
1000.1074950.8878770.3693970.5762830.4358710.8273820.9150460.4682050.9591020.1850620.09631240.4065930.8707920.0280150.4946620.9338870.1818120.1236950.6653210.6636650.5444160.9197960.6868240.627930.5187760.9727330.7971360.6712150.4443740.4027130.1569370.6200370.7467530.7637060.1316750.6533070.6850470.4290340.02910070.9398680.8382020.115690.7929860.6690180.2112840.3145350.04242770.06425280.9218950.3622940.229890.8916650.01257250.6418290.7581130.3407060.1271060.9548780.6352630.1534240.8097160.705070.6538170.08063720.7847760.3774780.9904720.8635450.8645040.7160590.7551450.7656880.5786960.1974580.5036610.02524160.0006273450.1710830.7961960.8297530.7840.4820180.6446440.9535080.4706920.4061050.6389890.2720680.3202210.2718930.5736560.3343030.4276010.5659920.9489710.5929060.5068240.3970110.06140640.918614

we can see that 92 of its columns were not printed. Also we get its first 30 rows. You can easily change this behavior by changing the value of ENV["LINES"] and ENV["COLUMNS"].

withenv("LINES" => 10, "COLUMNS" => 200) do
    show(df)
end
100×100 DataFrame
 Row │ x1          x2         x3        x4        x5        x6         x7         x8         x9         x10       x11        x12       x13        x14        x15        x16       x17        x18       ⋯
     │ Float64     Float64    Float64   Float64   Float64   Float64    Float64    Float64    Float64    Float64   Float64    Float64   Float64    Float64    Float64    Float64   Float64    Float64   ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ 0.131647    0.733765   0.795819  0.785569  0.774437  0.8478     0.949707   0.241036   0.352108   0.55987   0.113794   0.116554  0.810311   0.871996   0.0585215  0.959779  0.967253   0.757061  ⋯
  ⋮  │     ⋮           ⋮         ⋮         ⋮         ⋮          ⋮          ⋮          ⋮          ⋮         ⋮          ⋮         ⋮          ⋮          ⋮          ⋮         ⋮          ⋮         ⋮      ⋱
                                                                                                                                                                          82 columns and 99 rows omitted

Most elementary get and set operations#

Given the DataFrame x we have created earlier, here are various ways to grab one of its columns as a Vector.

x = DataFrame(A=[1, 2], B=[1.0, missing], C=["a", "b"])
2×3 DataFrame
RowABC
Int64Float64?String
111.0a
22missingb

all get the vector stored in our DataFrame without copying it

x.A, x[!, 1], x[!, :A]
([1, 2], [1, 2], [1, 2])

the same using string indexing

x."A", x[!, "A"]
([1, 2], [1, 2])

note that this creates a copy

x[:, 1]
2-element Vector{Int64}:
 1
 2
x[:, 1] === x[:, 1]
false

To grab one row as a DataFrame, we can index as follows.

x[1:1, :]
1×3 DataFrame
RowABC
Int64Float64?String
111.0a

this produces a DataFrameRow which is treated as 1-dimensional object similar to a NamedTuple

x[1, :]
DataFrameRow (3 columns)
RowABC
Int64Float64?String
111.0a

We can grab a single cell or element with the same syntax to grab an element of an array.

x[1, 1]
1

or a new DataFrame that is a subset of rows and columns

x[1:2, 1:2]
2×2 DataFrame
RowAB
Int64Float64?
111.0
22missing

You can also use Regex to select columns and Not from InvertedIndices.jl both to select rows and columns

x[Not(1), r"A"]
1×1 DataFrame
RowA
Int64
12

! indicates that underlying columns are not copied

x[!, Not(1)]
2×2 DataFrame
RowBC
Float64?String
11.0a
2missingb

: means that the columns will get copied

x[:, Not(1)]
2×2 DataFrame
RowBC
Float64?String
11.0a
2missingb

Assignment of a scalar to a data frame can be done in ranges using broadcasting:

x[1:2, 1:2] .= 1
x
2×3 DataFrame
RowABC
Int64Float64?String
111.0a
211.0b

Assignment of a vector of length equal to the number of assigned rows using broadcasting

x[1:2, 1:2] .= [1, 2]
x
2×3 DataFrame
RowABC
Int64Float64?String
111.0a
222.0b

Assignment or of another data frame of matching size and column names, again using broadcasting:

x[1:2, 1:2] .= DataFrame([5 6; 7 8], [:A, :B])
x
2×3 DataFrame
RowABC
Int64Float64?String
156.0a
278.0b

Caution

With df[!, :col] and df.col syntax you get a direct (non copying) access to a column of a data frame. This is potentially unsafe as you can easily corrupt data in the df data frame if you resize, sort, etc. the column obtained in this way. Therefore such access should be used with caution.

Similarly df[!, cols] when cols is a collection of columns produces a new data frame that holds the same (not copied) columns as the source df data frame. Similarly, modifying the data frame obtained via df[!, cols] might cause problems with the consistency of df.

The df[:, :col] and df[:, cols] syntaxes always copy columns so they are safe to use (and should generally be preferred except for performance or memory critical use cases).

Here are examples of how Cols and Between can be used to select columns of a data frame.

x = DataFrame(rand(4, 5), :auto)
4×5 DataFrame
Rowx1x2x3x4x5
Float64Float64Float64Float64Float64
10.786970.0499280.7733750.9777870.0181344
20.5148160.458390.05436530.4987180.791808
30.7446620.2306290.6136160.9594920.350561
40.003015020.361120.9339720.2519560.692337
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.0499280.7733750.977787
20.458390.05436530.498718
30.2306290.6136160.959492
40.361120.9339720.251956
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.786970.0499280.7733750.977787
20.5148160.458390.05436530.498718
30.7446620.2306290.6136160.959492
40.003015020.361120.9339720.251956

Views#

You can simply create a view of a DataFrame (it is more efficient than creating a materialized selection). Here are the possible return value options.

@view x[1:2, 1]
2-element view(::Vector{Float64}, 1:2) with eltype Float64:
 0.786969597221463
 0.5148164785458531
@view x[1, 1]
0-dimensional view(::Vector{Float64}, 1) with eltype Float64:
0.786969597221463

a DataFrameRow, the same as for x[1, 1:2] without a view

@view x[1, 1:2]
DataFrameRow (2 columns)
Rowx1x2
Float64Float64
10.786970.049928

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.786970.049928
20.5148160.45839

Adding new columns to a data frame#

df = DataFrame()
0×0 DataFrame

using setproperty! (element assignment)

x = [1, 2, 3]
df.a = x
df
3×1 DataFrame
Rowa
Int64
11
22
33

no copy is performed (sharing the same memory address)

df.a === x
true

using setindex!

df[!, :b] = x
df[:, :c] = x
df
3×3 DataFrame
Rowabc
Int64Int64Int64
1111
2222
3333

no copy is performed

df.b === x
true

With copying ! and : has different effects

df.c === x
false

Element-wise assignment

df[!, :d] .= x
df[:, :e] .= x
df
3×5 DataFrame
Rowabcde
Int64Int64Int64Int64Int64
111111
222222
333333

both copy, so in this case ! and : has the same effect

df.d === x, df.e === x
(false, false)

note that in our data frame columns :a and :b store the vector x (not a copy)

df.a === df.b === x
true

This can lead to silent errors. For example this code leads to a bug (note that calling pairs on eachcol(df) creates an iterator of (column name, column) pairs):

try
    for (n, c) in pairs(eachcol(df))
        println("$n: ", pop!(c))
    end
catch e
    show(e)
end
a: 3
b: 2
c: 3
d: 3
e: 3

note that for column :b we printed 2 as 3 was removed from it when we used pop! on column :a. Such mistakes sometimes happen. Because of this DataFrames.jl performs consistency checks before doing an expensive operation (most notably before showing a data frame).

try
    show(df)
catch e
    show(e)
end
AssertionError("Data frame is corrupt: length of column :c (2) does not match length of column 1 (1). The column vector has likely been resized unintentionally (either directly or because it is shared with another data frame).")

We can investigate the columns to find out what happened:

collect(pairs(eachcol(df)))
5-element Vector{Pair{Symbol, AbstractVector}}:
 :a => [1]
 :b => [1]
 :c => [1, 2]
 :d => [1, 2]
 :e => [1, 2]

The output confirms that the data frame df got corrupted. DataFrames.jl supports a complete set of getindex, getproperty, setindex!, setproperty!, view, broadcasting, and broadcasting assignment operations. The details are explained here: http://juliadata.github.io/DataFrames.jl/latest/lib/indexing/.

Comparisons#

using DataFrames
df = DataFrame(rand(2, 3), :auto)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.872630.7721680.575777
20.3809970.138050.670492
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.872630.7721680.575777
20.3809970.138050.670492

compares column names and contents

df == df2
true

create a minimally different data frame and use isapprox for comparison

df3 = df2 .+ eps()
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.872630.7721680.575777
20.3809970.138050.670492
df == df3
false
isapprox(df, df3)
true
isapprox(df, df3, atol=eps() / 2)
false

missings are handled as in Julia Base

df = DataFrame(a=missing)
1×1 DataFrame
Rowa
Missing
1missing

Equality test shows missing.

df == df
missing

The same object?

df === df
true
isequal(df, df)
true

This notebook was generated using Literate.jl.