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
191082104731010
22655685296
359974998810
451073624884
5242108191033
6721084810415
751765773310
847107101011095
910114966528
10431038443110
1146110736997
1210584336186
1361055545949
989161051034581
99022971087811
99110484457327
992233510618103
99338986109113
99418136733107
99588767141021
99641922713101
9971839551971
99887108339379
9998556477678
100037594643510

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

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
191082104731010
22655685296
359974998810
451073624884
5242108191033

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
187108339379
28556477678
337594643510

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

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
191082104731010
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
100037594643510

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.08758760.1680730.6535860.7641030.08834720.5846130.2431220.8260910.8768850.7138810.05189930.3099130.3340680.6952540.3154670.3206010.4670850.7277520.1391310.6050180.2604240.3550990.02021430.8933940.4317650.05781640.8845730.4998670.3624380.7486630.2590120.6480610.1060880.4869250.3212880.5788730.9313340.3996120.2778140.1969010.4020510.2757860.7644280.9613630.4174830.2150880.9849350.7553590.7582180.02344470.5970890.8252990.3278980.463080.6167420.280960.9025580.8155270.4438530.9479930.1588940.9348490.4970890.5823540.5427140.4272690.5828360.1260620.02321340.7444170.4558310.5374220.2804120.8292960.09953320.0538480.3901740.6207160.3932360.5049550.5561330.4129980.1483830.389110.5224980.0231910.2902060.2460070.8222950.4932230.1029180.6782080.2557710.9613170.3166630.3594820.3466870.8877990.6784170.195698
20.4920760.253280.5894120.06606290.2173850.8357860.5771170.5265980.9090780.3312470.6564230.285310.06992890.652830.6543730.6200650.5522960.1334690.9029640.8330730.1251750.8200510.8851110.5767970.5531480.6658360.478540.7079430.01828920.3718550.4413070.7988690.3963860.8918550.6348770.5776690.2139070.5459140.1478360.1468280.01881240.2560890.6863140.7885780.5698580.1142970.6810530.2885310.5399810.7967480.1264420.2045320.2444060.8132780.5564530.2120190.8181560.4425510.305810.9278830.3253990.4722480.5316770.6096650.9925530.6071770.2980060.6420770.1825460.1013390.4136470.7477290.1954460.03185840.2498690.2306310.5901370.6476630.1122210.3109570.9206990.9511880.5683050.08434530.6566350.9782960.8559420.09213270.6482560.4443280.1720150.1597010.5545580.931620.08367610.2925460.1653370.6564170.4467220.778291
30.4219440.587530.4136650.4644730.1714960.8172920.1155520.6596070.6334040.03302570.1108510.9994360.07823620.4721790.3057740.9990510.8695920.106980.9532220.4207090.9788470.995940.4789830.5299720.5126680.6129190.4555560.7293270.8299310.7153760.4297520.4406820.113630.8022250.8868410.9162580.4460330.3611510.9065110.5941940.278980.8446950.9887250.5438240.6459660.5883430.835020.3079730.8200680.7287040.6069070.5813970.8381620.8162310.2600710.2420150.3423060.4588940.6752440.2958040.2536420.02146990.499260.7467010.01395390.9092190.6205090.6667540.626310.6438920.2026460.4099970.4903270.8100760.6176840.9108270.9491740.2526130.2312880.2834540.8248450.004993590.6547340.6621360.7180060.5298910.7062830.7392930.5437570.9825230.526940.9398980.002766990.1439110.1863360.7857620.8726730.2901670.4317070.77876
40.1952070.7212010.7046820.7996730.985990.5474950.3541340.846550.01796830.7070370.06965260.5231570.01402370.8867630.8408160.1297760.6460680.1904370.7634570.9324130.1519160.1856260.6224050.7696960.0009611010.5404610.6155070.494260.07354630.2783260.6631660.3184190.1057050.3393360.2167360.639980.9223360.9301520.6810740.3393780.4449050.9887460.8500450.5258070.2505570.7463480.6315940.0868680.2434670.1262810.2847960.2340450.5660950.0288190.9860840.2151810.7415050.2120910.184190.7283370.2977650.8579440.6320460.0946890.158490.9018320.8108910.2907640.9469140.2134780.919510.4655310.9784330.3229920.1295440.463150.1077830.5493790.2662360.418070.6633980.7251130.07756180.4668790.4108530.6176220.1905570.6557390.2124130.8899910.571050.1112320.1367030.4450270.6176510.9991280.1509940.2821680.6076620.822821
50.6831490.06026830.6630170.4382990.6860480.4983320.3464350.1434550.5247420.1216990.6199330.9464590.9762650.6900510.7323020.7639980.1757130.3815450.4433760.2453420.8868730.60920.06628340.06035360.9550840.1228440.001579160.6427570.03743890.2376860.9366050.6394990.2439540.4487870.6496260.907670.2451690.08961920.9845770.2603810.09332050.6757480.4895110.02536060.717470.6494850.1804670.6862930.4714360.3842920.5514060.7791550.4068150.7065660.2554320.6358640.8614990.5131780.6438330.8600440.9724180.2849780.1619870.3794580.06194670.8364130.6602240.7790440.1133880.4007770.7930770.264840.7681720.4247250.2481410.6349350.6011290.5233810.3748690.2117210.5973230.3242470.7366920.3171210.07170370.05652430.6632730.002851330.9555880.3035450.33110.1243990.03787470.947760.7312740.0002898890.6730990.4446440.4880270.150527
60.2883430.2514680.400350.4478210.3116920.9109340.6080260.5840510.9169870.1213780.8398910.5478120.9344940.4317230.1905860.8805850.3384060.2501420.4710310.4105630.66930.5184770.5653890.1093850.6673360.7122540.02845140.8584430.487490.3363590.05269430.5329430.5273840.4966620.4341950.5743360.7583130.5130030.893310.7680550.2170240.1145660.06697750.5353940.8158640.3005580.07936920.6260960.1421860.5013980.3079890.8344350.423490.4288910.5157930.3716390.02424970.3656040.4927460.676430.7596280.8805540.1596650.3729470.779710.2132050.6796830.1006290.991750.4883150.1785610.8443970.792130.6503960.8660040.9698640.8602990.3831560.7379740.590180.751230.7566830.6610360.9032180.6623780.4255920.2588190.2295020.2261390.6006840.7162420.8928870.1030280.6214480.7229170.4951650.001757140.4385360.4008110.00723
70.3442530.5498520.2303360.7726370.2350590.494670.1263080.3802670.5275780.2364570.862220.3814140.3624930.4545980.632990.4578740.09182360.9035010.1784610.4456630.1155410.4092270.02282510.2040030.05239350.4812790.5015960.5526610.2931220.9147080.3832510.9700840.5806340.4208880.1021840.7621680.2141350.7504510.7823740.4744220.03013370.9983480.5731270.1357690.3861520.8945740.2173570.4086070.5259740.1048050.8623640.4668560.6264290.380370.224140.02253960.2790050.5414530.2406460.1622840.3926070.9923150.7387090.8559530.478920.4474480.9151250.3467950.5765540.7434780.6288750.7164360.8556010.08494680.9949750.5233570.5753330.1552020.6397280.09587360.4974930.01209850.3302180.5699630.5975340.0321460.6799250.1240610.04352020.9294310.4943590.7234290.3628660.2915250.7787680.371250.7428230.3887230.3533280.731357
80.6160120.9315450.746240.8216980.5684320.07371970.9668520.02100850.9780730.04666520.9037380.4829960.1373670.1412910.583280.4955040.5684890.8497240.8027540.1724570.7141430.5557930.2043140.5327280.616780.9129320.1963870.276050.4473350.6306490.08594910.2344210.09434620.3929270.4808580.3913230.1402870.6870570.3212250.6509570.9968430.4674130.9462530.186330.2367040.5145830.9121660.308460.8014620.8359250.4159570.9242690.9793070.528990.2097680.882020.4799760.3595070.4488970.05747520.4511310.8255140.2773110.490250.1743320.2133390.9603580.6866680.04378530.7499880.8347880.7913090.8434980.8864450.1008240.5935180.1270480.2570320.02169010.9832050.5723450.2091460.29990.6573060.6665040.07616450.8362350.9458930.06008740.5077310.1157240.266010.4015410.3197510.107460.009405240.9383920.4060150.4154550.846335
90.2488570.9728120.2538690.1661830.1366810.9069340.101690.9036650.02574340.5744890.1334660.3625160.0546960.4306860.9542250.604790.6869250.5968030.5339270.2735490.2434510.4404470.9761060.06465230.06868490.07325790.3340930.9430610.3898140.8603630.4591320.6968620.2063720.4839720.532060.1973470.4283990.7679610.3580570.7244960.1541090.3803210.9367270.9685170.3492670.8025750.9020640.01563280.2898090.6111520.6397210.9402290.7359960.2750460.1157090.5403580.5954480.2862650.8134340.1581610.5309450.2250990.4398310.7572920.4815070.4298450.2753180.6810880.2578440.06768510.6109110.7175580.6138950.1157440.5143260.05136620.1329590.9105040.9270140.566160.9629230.4070840.1737020.5074920.2852330.9666970.3694030.5240590.2163920.4273020.8777780.6024250.8976490.9701660.02605970.6369460.8694720.6907810.6635340.906389
100.8593510.01437910.3348760.7351890.09071770.7120240.8782360.5930830.867710.07975680.836420.9858730.1435530.5607330.4452380.7187380.08824110.4751390.2268160.7547110.3757020.6431610.3102740.4039790.5567650.4173740.2094430.8604080.7928530.288170.4853730.7683680.9537220.825110.8978070.02664740.9935810.05564110.8047350.4786150.09201430.8861840.9481590.6025710.1029130.4382140.5349890.9342220.7645030.6395750.7607140.3129990.6585340.273270.06805090.7343910.4421080.6121610.208420.7157010.2905650.4611750.8451430.4008060.1568610.4456810.7495570.3900650.572410.7454660.748790.2002920.1023650.5895350.7853620.6831850.3342350.8987520.5264720.2505630.693070.09231970.6097820.3822490.9686960.4287110.4130830.08895730.6949120.2232170.5395150.7074340.4910380.4046040.4368670.6195490.3384830.1383290.1538470.0435587
110.2850130.7367480.2261770.09057390.2660270.2766980.6941730.7194510.005706350.9416790.5513770.2442930.541030.4888850.03696810.6330160.2304070.5395450.7979840.198560.4973660.436850.7918730.05097860.8506830.3551530.625560.1132780.01130450.6433260.4347350.8689570.7891080.7335630.3769190.642340.1146210.2860380.2383270.4328740.3228220.5689980.7688620.1063690.7128970.2752820.966360.5253730.578150.03439820.4222160.5348410.4166560.6009120.1627750.8826310.08952570.3783020.3904590.8779010.9489910.4337910.144110.6465170.1344120.6819790.955890.4886190.8526920.5058270.5083450.1746450.5830790.7735720.3703820.7954070.4421460.5302260.3818290.715240.8253470.3144540.7799550.09108970.6521160.8153740.1557470.7975780.2567960.1700790.1347160.2552760.2518740.7294360.4440670.5338980.7870160.480990.9793680.323764
120.7152970.08651780.2158140.4260310.1626720.4097060.677280.1687730.7184580.0170590.04016130.6782820.5392490.2779420.3276050.206860.03300750.6463570.2335940.7165910.2645440.2706860.4337030.7954180.2785050.004862960.1598040.104440.193850.9888450.4771520.670460.02869290.9449280.8679740.7490590.047070.1004980.5844140.9150680.7513350.1904810.5776120.6813740.3887380.2398880.4338910.1880040.9953290.07650320.5575160.9615130.9577340.4576510.3237890.4187980.7008360.4302870.6659760.5632840.688980.1719350.3979180.251360.9888110.8819850.4930390.7959780.2819130.3676290.3107390.2225780.4065240.5542030.1459810.1454980.3804610.6609810.5914350.7402220.8210230.5992120.5487620.3224050.533620.6390550.02500920.7804660.5103320.6375010.5671780.8086980.02301360.7205210.583370.2219760.8950490.8593620.3487280.344521
130.99070.03667780.7522930.3924320.218560.03535820.3166410.5180510.5068750.1107380.8406640.4487140.5128310.8687010.4357080.8219840.610250.4437370.1698110.4049380.4055390.5538520.1639590.9209930.4237570.3608080.2127430.4243850.2976080.04957130.6211010.1701650.3867480.6044160.5558420.3412730.8117120.1238820.80470.5093960.03015620.8169380.8885330.4967160.1607870.1268860.1948960.5144750.8143440.01752570.967930.1566480.2004970.7646250.008631440.1810770.3506090.06819110.5475370.1629380.9778720.3318560.7347280.8475610.6941240.5028460.3260710.06311960.8811150.7973480.8471090.3902350.180780.9981190.6059230.791380.4676230.7840520.829560.6410590.6691530.4015330.000699580.5286440.382780.7081740.5741910.8342860.8303440.7259810.860650.224840.3781950.09240810.7885580.4647780.6829540.6442240.9520650.204615
890.6089950.289460.3623920.8579780.6159180.5686420.7662210.201130.2932870.4141070.2026450.4350390.9921220.5448590.947050.8075220.3197880.8740240.8928710.6108480.909610.4540510.2251090.8379380.4013090.0947460.6771940.6482890.28510.2072790.6711760.9438930.3433040.7331480.9950680.02841850.5061420.3441870.389060.04216670.624360.03011990.595690.2326930.6604580.4662070.7874580.7467840.7947660.1167780.6179870.004481410.4071950.1886570.5915050.7372950.7921570.1358160.178610.4823560.1003620.5833810.9817810.5948820.7676930.6676230.6750750.6564130.03440940.3653660.6041440.9938560.9019930.9296710.0004888880.9546670.3489550.6588080.1038660.1154390.3195240.4853660.4036050.3580610.5274370.3259570.8574980.2330180.293150.7216190.2795140.9019040.8565280.6304350.4087770.4282630.3817610.1966240.2732140.84329
900.9441820.5144430.08274280.07064970.2707220.6155350.2230160.001996390.09259650.9770880.2923480.4463850.5882670.7332820.2217170.2122150.07611390.9239940.900520.05094060.7833630.8279930.9204540.06445920.3333550.9098960.2166840.5230830.7872460.6521860.2516290.6258520.2976560.3232440.9142840.1957340.09277090.1607230.2067070.1684840.6559610.9189280.1139910.8661170.5109420.2660970.394090.896880.8680770.392970.3972880.005118080.9501730.5283490.2080910.5059310.1362890.2588160.185690.2986060.2807950.760140.09013610.3509450.1059030.6825630.2839690.6730470.8811390.7412280.07438760.7125340.4450470.8503950.7555570.8858130.5049690.2116920.9634780.7167970.521180.8012850.1209860.7514020.3119550.2831050.3143820.8798980.2570480.02040060.1532120.6998640.6390550.6990790.6600380.231790.2879390.9629630.1793870.303886
910.4909640.4959120.4438350.6251630.2147760.4192730.4996990.1483160.1169190.5261660.717030.7146540.9244330.7425930.11450.05900060.3802140.7142530.8754420.4050310.1208920.5780490.5255210.652610.7729740.152380.9103010.6672620.8471840.5154380.7231340.8591640.615620.6672020.5158670.2622170.5575420.9418930.1854970.8805640.8488350.712090.1444150.2614490.9537130.3281520.1183990.9617230.2201170.7371220.7015620.314880.7399480.4177250.9564340.6623050.2184660.4629420.7466130.08753830.01688750.5458160.3292570.1475640.4642590.1474350.7104980.0733880.7897790.6754970.8684660.3007940.9380330.8818480.4152420.7287370.1129980.631980.004629540.8432650.5067620.08678030.2912930.6467260.4256830.8966860.4277110.730760.9983550.4993750.3121380.633940.5721450.1157240.7520330.6187720.155650.5526480.4336390.0465949
920.1375420.8759070.04654340.7915370.9001850.692470.1912770.9565160.3621630.2881170.5652510.3381680.337630.7804210.2702490.6968680.723790.002576030.2798150.3064150.07790420.3771180.7616470.4264020.3318920.4537140.1621760.8097860.1930850.7797790.4106980.7170640.5756080.1021240.471910.8813290.8291320.2113360.02524720.6440820.5769890.5295280.6483590.8812350.8620820.06025930.9619770.1501890.6529070.3737420.7571710.9573960.7931190.3474380.6380020.869040.3788820.9115660.4495190.1502060.4681490.916930.8182080.434420.984120.9532890.1560440.5173920.9553610.4695550.7399550.2291570.4782070.9431740.2074820.9582520.999210.7214310.2747010.8485410.5001060.7773290.6339140.7878820.01357620.8438430.815070.5970040.5023390.9850260.0683420.8096270.9240340.0595510.2084880.03432980.3649330.7144760.5932440.600167
930.8380470.3027860.6951050.6598940.6882030.04748130.2885520.1409170.6555320.3821550.7419850.2239960.1428370.4946220.7064910.2105110.978340.2460650.1886880.3244650.3790.4741020.3891940.6868990.6161440.4107670.4672190.2263090.9833850.9908040.6253640.05930960.4706010.3055810.1572720.2447790.01945280.4894590.6643810.9176570.07580530.04001680.146410.07430760.2204610.004566010.8740130.1078230.3967460.3426070.1150110.9640890.03832440.6348740.3040180.9411550.0417350.2163780.6862260.8736660.04925470.4189330.6160530.8565780.7764140.928930.1148620.4173340.1328810.5468850.6547890.0273320.2354020.7697810.8680910.5172050.657890.7233020.6997580.6395630.6758650.1164360.2514350.5925740.7448230.842480.05359480.6467810.2345910.3185520.5887720.6799230.3538580.8688050.7818610.2310890.01532660.4058910.1616550.936259
940.1184710.8274480.2368290.01168190.03757490.274970.92070.7214610.7772380.9695690.3931610.07227490.6674910.2071040.8708950.6010730.7626650.01300720.6682510.3499560.9860860.8558110.1263950.7105250.8603010.5118010.3951270.6773310.5175990.09500650.3074750.1692140.1516320.3888070.6163960.4025710.1013260.3985550.5809060.9301070.4027180.8252960.3335170.06817150.8400850.7582490.8446680.185270.7741540.5701130.5260220.3822610.602640.7620550.1517760.7168860.6284650.5473370.2358180.8991430.6405770.04607550.3028290.3475360.1298950.2105390.3902980.7389690.9909880.4996190.8432160.6825070.4355480.1617010.06703330.06316460.5671190.3862690.1781780.9637810.07277450.3393510.805610.5138940.4041670.8783910.8684120.301060.2465720.742250.7839280.584130.3429660.6077450.4782960.2719510.6925910.4318130.7917580.734014
950.12430.4493230.8505560.9924340.9175590.991370.9498110.2098420.9958380.07510210.9677630.996770.6976120.5563480.8797470.8587210.3330630.2947250.1534730.5288630.3595980.7603360.6363170.6166730.5609840.1219420.952670.676130.5306580.6029370.7884340.9145590.6150440.3732660.9006230.2654770.9689930.210560.6336630.7651140.5854840.1055960.9864170.996810.3495020.08070770.5669480.006316130.7362960.4089150.9031170.02738190.5516950.8141460.8329330.9546290.4053630.1987620.08529130.7440820.985680.07397960.7031260.2597530.745430.7952330.1476590.6255260.9005350.7403850.3858130.6267550.2762380.9882320.1990690.6099260.2501090.9723770.8390860.4469450.958270.320070.8515040.1623260.9266060.3340070.300950.5861980.4799260.6794790.9819680.3212160.3121930.6706230.5396320.7287950.3244740.09705250.9532840.658982
960.5011250.9834080.6986960.4841030.4830350.750450.2015240.6019090.0003896670.1363760.250920.3798970.8706660.9281630.602390.7853870.9771430.6573140.7310090.9748060.7796350.7672920.2355530.5677510.3707350.3495870.6075090.3997130.7537480.3240070.3741870.8649680.1693340.3560580.01124220.5526660.2455790.8651820.309030.7669090.7799630.1232380.06611650.204170.2522280.5386890.9071920.5025910.8123960.7342050.5455630.188140.2932150.8063280.7461350.7122130.7019930.08107360.008358430.8788440.3211290.747780.7113610.1806810.7200070.9239590.648620.4942870.4327140.04028560.8473080.7004270.9332430.4356080.6571960.7209580.8837580.3335170.4596620.7880130.5132780.8189080.7047740.01374760.905810.8319250.5086360.1654960.4975590.49120.921230.1030140.4233740.2536990.459430.6860970.6340440.1032270.5450160.238553
970.5707650.07930050.4643210.6087050.4961150.40330.2785840.4693210.3017550.009208980.8602210.2919020.7198130.8982970.1087960.07637540.4276210.5675050.008671020.2145820.008989170.3289830.3638030.4337310.7996330.1386360.3946540.1084210.5466260.1009720.2192940.4403430.477640.2102530.01879310.8058650.03948530.796930.8182620.6565760.55780.3354130.7580420.9588130.1925730.2507140.04952530.5983040.05932150.2838560.3033990.4455230.3732250.2120610.5641750.6791140.4838980.9204880.42120.01869240.3341020.7139560.3988810.6852470.7068480.4734190.1926410.1368040.2160650.450810.6306690.6218430.1673060.4002050.5405050.06700680.5428910.2831390.3208820.5844110.150170.1331150.153890.9790630.4235620.6818130.07996130.3010240.828480.2881170.6785949.37698e-50.08882490.6795630.3821310.2377840.2787060.02972740.6849580.636808
980.1178160.7732060.6029990.7864620.08653110.2435850.3619380.5311930.5070560.339240.9840980.2325930.8834690.6476130.7915630.8482030.9923590.3140830.5261390.139060.53290.5859540.1292490.578650.3690010.5031490.2969060.2441220.2235970.7804560.7098750.6199480.8328740.6598260.5431540.3729030.6113170.5902120.009777060.8860960.4586320.4158130.9113290.02131530.6026520.1956210.3537610.8256490.1767950.4240320.148240.3673860.147070.3633160.5226680.0866240.9215970.6156670.3481610.8455180.9803560.1189440.3526910.2611540.3785240.0156590.7692750.1443760.1418070.09093030.1965910.1582880.3989310.7003790.4957880.9925520.4246190.5838210.5421120.4430360.4684040.3859810.1403160.06615420.7231710.2107350.6189840.3623310.2976880.4167710.09434530.463840.9186390.05137130.6545330.9184610.1647850.8255040.08655610.0966211
990.06081780.4249460.3832390.642570.8201010.003506120.9238480.4918980.6248560.3807090.2767560.5861450.1864040.5733570.2276460.6852170.7137290.7378080.1311170.6552370.1466680.9944470.2466840.7849850.7214290.550620.4689450.8899380.9243390.1090920.9659880.1147190.2454170.106330.1630670.6010410.8579710.3407890.446470.2535780.3362710.1344960.491780.9108960.8969850.590250.1217940.112780.3824490.6829870.8062560.2770060.9030940.5902150.2620740.7952180.4579090.8232890.2225150.9945170.945710.4158510.5184490.9562010.3732190.2717180.3113950.5258390.734840.8764070.5957730.5079010.8286380.01471030.3181770.7960490.3017450.1584120.05272220.8643080.5604810.2986770.640470.8871080.164550.2448220.6820760.5847830.9423030.9804090.2724030.7230580.754870.5343670.8268810.5121990.0974450.15620.9487640.968325
1000.9182110.6731610.1018050.8397450.3691580.9783850.1868490.4018760.7404730.5922530.5780840.7156090.5460030.6432510.8662210.177040.6572760.008289680.3349510.2176340.9447780.976660.4633690.9085520.8876380.6730010.8138310.01877180.8598810.1396160.2169550.836440.8647540.06505540.731790.5027390.995110.1882430.9800560.8797420.05736140.06417320.8985010.2118250.06856080.1076860.6720630.5322120.2894820.7440420.453890.3585190.2907030.7661330.5209670.5487830.7011630.7624350.4341830.59460.1340050.4260010.9400980.4735930.2690650.4090730.4570250.3238040.1585780.6637880.6198120.5057590.03594170.1132760.8953480.8766290.1478120.3039960.6969580.6038980.912480.4012530.1542220.6666630.003103070.5411020.9048230.4493910.9402590.4914820.03943970.6032750.2587460.7232770.2079630.4233160.8304320.6228460.8679520.0534483

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.0875876  0.168073   0.653586  0.764103   0.0883472  0.584613    0.243122  0.826091  0.876885   0.713881    0.0518993  0.309913  0.334068   0.695254  0.315467  0.320601   0.467085  0.727752  ⋯
  ⋮  │     ⋮          ⋮         ⋮          ⋮          ⋮          ⋮          ⋮         ⋮          ⋮          ⋮           ⋮         ⋮          ⋮         ⋮         ⋮          ⋮         ⋮          ⋮     ⋱
                                                                                                                                                                          83 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.8854890.3835750.02152190.8615980.14877
20.6043850.5897820.4094150.01168710.987552
30.6936090.7410140.01214880.04088280.233027
40.1161940.7447130.7525890.5155980.622336
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.3835750.02152190.861598
20.5897820.4094150.0116871
30.7410140.01214880.0408828
40.7447130.7525890.515598
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.8854890.3835750.02152190.861598
20.6043850.5897820.4094150.0116871
30.6936090.7410140.01214880.0408828
40.1161940.7447130.7525890.515598

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.8854889508671497
 0.6043846346146554
@view x[1, 1]
0-dimensional view(::Vector{Float64}, 1) with eltype Float64:
0.8854889508671497

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

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

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.8854890.383575
20.6043850.589782

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.2104720.9715670.67789
20.7150910.9083230.925757
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.2104720.9715670.67789
20.7150910.9083230.925757

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.2104720.9715670.67789
20.7150910.9083230.925757
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.