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
11836678831
26521138562
37682879467
44641263358
569756210517
66242789174
71287213517
893622106411
961376627107
1094542910322
116577339253
1231069299632
13681023510541
98945485871079
99025481066389
991102315771054
9924298123987
99374110917797
994975210108614
9953129115389
996109310972371
99783424121095
99892810399927
999107481056377
10007393366694

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

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
11836678831
26521138562
37682879467
44641263358
569756210517

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
192810399927
2107481056377
37393366694

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

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
11836678831
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
10007393366694

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.7687290.8254260.6153710.1618350.3178810.7610320.7264780.3742740.9665580.1029550.6045280.5055350.8338560.5118110.4068540.402880.8013960.6056930.5216080.6188940.1309660.8662410.9015650.4872630.2138560.825250.4250160.5068880.5480510.2048740.302040.08818650.05956460.6898480.3816170.4437120.9097070.3496490.6615740.6401910.9053720.2613740.9998960.7153210.3988670.05313740.06344150.3012410.9692830.9739390.7423630.05090030.7672050.3962080.2875270.8234460.7589950.9008380.5668560.727890.05487040.007419460.7774760.1323070.0119890.8338050.08591250.02227580.4237980.967770.3221540.4498360.203160.4310410.5445870.2978090.3408320.009928360.5813670.3494570.8758610.002059470.2660810.8371780.1893960.1363090.5451710.7937440.926980.544620.6331740.155770.8487230.9210860.3595360.8284750.2225930.6313660.3732930.19219
20.1598440.02408550.2978790.9392070.6233660.8523920.719490.7135990.4289110.2943660.8391120.716870.2344960.3524610.4660920.9065070.03550950.5688510.3245190.8347140.0636410.7637470.9239130.1622360.3279830.1359450.9046310.962960.49880.5188150.6346030.9710050.7591090.9091890.07237380.9580130.1192570.8516840.2949620.45570.9351290.6048230.3004120.658510.6941360.9993660.9715060.6809290.6644240.3526730.4081160.4162820.005399930.7436350.1221220.9660520.6912140.2603850.9484660.8039720.8995990.191720.8157070.6236130.8428690.5687940.9359630.8819970.8186090.9531280.3199430.9801720.2818630.9752530.2715970.9050190.9895380.1361880.6712630.9071530.7673480.3535370.4237240.6494990.3980910.8822540.4236320.1980820.1029620.555720.6329330.03254230.9552790.7302980.8477250.7183820.3991210.04222890.8228350.872737
30.3077470.6248630.8940280.1702660.6889650.7190440.7766210.1228840.7932410.04853180.9237280.08702840.766190.5717370.2311620.02808390.603350.4455660.5593330.514830.8021290.4766330.9221230.4194570.06433090.8225040.559620.375970.4586320.5327170.6047820.1858510.952750.3396160.5524450.831140.5436550.05236970.8247810.2775850.5356110.2935320.9993010.6737020.9334740.4239250.8083020.4037340.402940.2109960.681120.628310.9067670.1811350.04199830.0004222050.02980630.2605940.3790680.1155580.09792460.8982540.2721810.3272510.8754470.5972450.4966490.3230960.7644320.5869710.2169030.303210.09616280.9934280.8204030.8818220.4835110.986990.4082720.09761880.02223870.06872130.7578870.5746270.2735250.07860070.8071050.2726430.1310090.2212580.03832640.9006640.8389710.9877020.6028790.1466420.7239860.2756340.6339610.262744
40.5942790.1575690.1829850.02413580.325630.8323050.1617380.8801690.06985940.8774330.965580.8350750.1033970.3999640.7217550.02441410.8568630.1283020.361150.1724190.679720.9704220.738740.9631550.3733110.8008310.6005240.2710950.5056430.2174950.7529920.2261860.3339640.1254480.879840.8295780.8595530.2494050.8281330.08128080.3495810.9420870.2822540.3184360.5584090.8952620.1747760.9088590.3636790.04683890.404120.03974060.5241290.7946990.358110.7527610.651270.8783690.7608460.2206720.01922530.5253130.2813350.9574080.597080.4506940.4973780.131430.9660620.9108520.9690350.1900760.535330.9793880.01364490.5754610.5942930.3062090.9172420.7071970.9387210.02805280.395710.4156610.2954970.6344380.1239870.9888630.5850620.9300620.7313580.410640.6019070.9419640.6043710.4861260.4963370.145190.9178140.491149
50.2912630.3441560.8839240.4260270.1806590.9257790.6985880.6361330.6316220.3127280.1968350.9655030.9252170.4181510.5193940.01443630.3673920.8813770.1041980.1144040.5801830.3787240.09506510.4638880.7258490.1124390.2317410.5188320.3173120.9502480.2259030.2087480.4064610.327070.04110380.3202910.08165510.6191490.4299630.162480.01310930.3367760.9879880.54060.426330.275980.3808960.6262990.8176570.9032560.4575160.06755240.7438320.8804640.1016170.2475280.7181590.1207270.3572280.5938260.918430.5678680.2579520.3643610.9063930.05863810.6890540.8905810.3214280.005949910.4279860.7451190.5159250.4812290.8043870.9417570.2388160.2703340.5651740.887730.06457750.570850.1586370.2779090.5107130.3154530.4290410.8664580.9104020.09691050.4133110.4308520.1173370.4772430.9573070.02561460.9091490.5491050.7308910.871694
60.7143670.3439990.05566420.7354030.08354490.4340790.9725920.6323680.01043190.9476650.1262570.1348020.02158550.9930480.9169570.3859490.231170.6409010.5153560.3796390.2516010.9519640.1567850.7640830.6787930.4101020.6842230.3919660.6154890.8867270.4283620.8426960.1280680.492870.8181860.8703810.9542140.1442050.6045670.8835460.9236270.6791270.948570.9995880.2077840.6256380.739560.2415040.454680.5085970.8760370.6102530.04024740.2975990.4319270.905380.8389310.4461340.7313370.1621680.8725470.3763760.8404240.8543470.2385460.2099010.904170.499150.971970.4604360.3670030.415940.8217520.1873440.5198840.1201340.001518390.007495990.5039960.2185320.8487160.3623760.9612060.2965210.7391740.2864930.837030.5646350.8254020.7731380.5122460.005671310.7239850.8752790.7272450.1655190.3467980.3742430.498610.801762
70.3794080.9911390.4349170.2556250.6303520.399870.5957840.1253550.6102060.9652230.2746940.2724460.801610.5289240.7246170.1339810.8900610.9210670.1707110.4888420.06221710.7070910.1345820.8017180.7249310.4184210.9676630.6747480.0976260.05921050.8041010.3383080.4190390.8536010.802810.7202190.3692490.09598430.9480540.520030.5251260.8534820.2272340.787350.827250.9413910.5982450.7928020.6335390.8623180.3388820.06844850.7812710.1744780.4469250.965950.193530.1359320.6832530.6385370.7100380.8451470.4005450.0002434130.244850.1468940.8437450.5897920.3119390.9816940.7086480.04899110.166320.8956760.3278220.03778030.3492110.7232260.6426370.3113860.4087680.4020230.3324620.260520.5853270.5839590.1508550.3108180.1201320.9013470.3434520.2585170.2117220.9472090.4279160.556290.6733160.6010030.4757620.448629
80.4077690.2514530.2511730.8327950.8452480.9940470.4739080.2817150.1183770.5631660.7534890.3196930.07285590.2458150.6211980.963070.2287920.9513340.640530.02661180.8738640.7592960.8271060.4645880.3924210.07911350.3649790.1721360.6776350.3204550.6914940.4835940.4890810.1263070.7166870.8154960.301980.5107830.1916430.6304670.9977470.7363640.2724910.2547190.8505750.8080980.1497540.09875020.1171890.1826460.7044350.1805970.9421680.1924490.8956410.03339730.708140.5606120.7579990.7660710.1468130.4270810.3250340.1826770.4552320.5349990.8993020.8298820.8494640.123740.3832040.4652960.111290.7279540.2101130.6631960.8544850.1198930.8135680.6732180.5411270.274970.4577140.3435680.8465310.1870540.1891230.003556690.1902820.7807510.249780.3443320.3355640.7282820.7362090.05123240.5325330.1990510.4762890.722386
90.4674930.8890170.3937950.1637680.2774760.7141710.07238980.8529290.8137510.6272360.02178780.7589130.03069190.5319540.959030.774760.5375110.4846280.1090710.470110.4417590.7756830.01706770.4070990.3643550.02071330.4330940.6116760.3317430.7545930.9915010.7020930.1379290.7558690.1070490.4494160.4643630.1345730.5963540.1005680.1684060.2689220.7940970.5232570.4393530.1131940.4831510.1547540.7694210.507720.1233750.06805190.01252470.6205640.3633890.3617240.7624070.190910.2687550.9313280.09718110.08987160.5568390.07245410.2674410.8175270.8365390.1459380.5856230.4012330.1189650.4152910.9824940.13850.3887590.6989820.9665540.2390380.6070740.9261720.5711090.6104420.1413740.7637470.8463470.7631690.229110.4134830.9897790.02641740.05954360.3614150.8269670.3476330.5538940.3831190.8647520.95480.7611320.649906
100.003127760.6567310.03378520.007712820.6722640.07260790.844330.9327120.3255020.1818220.9703580.8485940.621110.4469310.9921810.4759290.4945780.8871340.4193070.8021730.6986370.4657350.7964030.8630630.8880310.4464810.9480210.8173260.07652810.4194130.683620.4094590.4843750.8967370.7138290.9637810.0946160.2424430.9203160.4084280.4303020.7435980.1249010.3055590.9585650.02364160.9548350.9720610.4916950.7782580.5233840.8426440.4185840.7963760.05314780.876780.7099950.09505060.7329430.6787040.07304980.2019880.9655360.3440690.1170770.7629920.7445440.3532960.5727330.8796720.8878550.9268760.2743610.7871460.7897270.4085590.8146620.9288650.02509080.01986730.9330320.09281510.3743860.2310790.4575440.05676590.2459560.5467630.6184850.1824250.2863680.5013010.8039930.2412560.459070.8069080.1963240.4559220.01581050.553968
110.1988970.5982530.3281330.6877380.425510.3425410.8146670.466670.8357820.1117880.4628920.7616050.6955140.07057570.4538930.7453140.3124370.1734280.09400210.1671960.5493290.1267370.4870560.4539330.528640.7932820.6744780.3825270.8303730.05934810.7546540.7791880.04283390.3628250.7149470.1091270.2231540.9515840.5340120.1916490.3729250.613910.2128760.6187550.1072780.9397750.6978750.3341650.7242080.4641130.7036320.7234820.2644210.721420.6102880.3655710.7963920.444290.2692720.0302860.6873630.6807570.3331220.03121060.8748890.1004320.9447860.1915280.5401280.6916670.6220740.8818370.320150.8499190.8287050.5316820.3048870.8035780.363010.6624270.5160530.1784850.1574310.303090.5831420.859530.8786210.1924660.7118380.1370230.3698630.6416370.9187880.08516750.6647440.8269210.5793550.2140520.08789570.925011
120.7770930.2034690.5052440.6860830.7471330.4825490.6027760.5742520.06752090.5159740.5643360.3230390.9567480.6224450.4175360.3092980.1123140.570020.6719470.6187090.8743670.2600040.6844130.1688310.6152760.09713570.701810.3108250.3182410.2424370.8364260.7556370.5469250.7057860.3072620.8141570.25360.3624960.2301380.6692770.3859370.90130.4953930.252810.5665540.4608930.1290520.9205020.5867370.009350160.648880.1327090.5813920.6924070.2358950.1783760.6279290.7625140.3572540.3658820.6522570.8940230.881540.3335590.9457650.8154170.6500230.624740.04138410.4820360.5342050.4372320.1474320.6767950.7412820.4346750.7220330.7133860.916090.8227560.9268430.4244660.7344930.9687780.2832450.5571090.01138430.0557950.4110520.9830820.1208770.03169620.5734630.2216880.7533550.8810650.05111860.896140.5773650.691531
130.8157920.8844230.2731720.4477110.8641660.07236440.4531560.943220.05289990.2513290.2919860.7490060.8706050.9060530.9444940.1056770.4996180.7895280.5631350.3541720.04193160.157270.8680340.5988320.6693270.3246850.683510.1493240.1796520.3050150.2868760.9331790.7344540.0196490.8060430.9757530.7258780.1726460.7840260.8951080.5218070.7668760.1582530.1191590.300020.5523950.4312880.4430810.7121390.08288430.1831220.4078260.3710350.5515360.5073550.632560.5501280.3950610.8400330.35720.1796170.35510.9375420.6062890.9735140.7482810.6350370.7109030.8102420.3604190.5738730.03175790.1068790.1223640.5059790.5126350.2839650.1024390.3500350.09349830.7738850.5069350.7981670.3489670.9135530.726390.8330030.6598750.480580.01693350.5432310.1248490.8606290.1793310.8664320.5428620.05432120.9787270.8410830.950109
890.6894060.781950.1716350.3766780.4518490.6573120.01625490.9645220.784730.5282770.5641890.4937930.6099370.7969150.4908980.1797130.663910.179380.573190.4195080.3688740.5586090.9945580.4905460.1285150.1405440.596160.5093190.298570.2148550.2461730.4645730.03525220.9806240.8230620.738890.9102050.7493080.62720.676660.5918240.4936290.7039140.2698290.9089310.8136480.7690320.0295160.7324250.2049240.01186730.3657450.7103970.9369180.1738760.1719730.02069590.678640.7274030.8754380.2072170.2734070.2094830.7178210.2155960.5632370.2910610.5953390.4485750.3153480.2849270.5496250.968130.2690420.4918280.8421040.6856190.4676940.002035430.149890.731580.7165550.2322340.8067590.02679630.3699690.7057530.07033970.4954360.003903150.6733550.4184560.534060.8108610.02943750.9915450.1934410.396630.3537520.573567
900.9570920.6381470.5620680.616840.4761430.4095770.5457950.6774710.5737380.5901670.3157690.8398570.7524580.4877820.4540930.3453150.5474230.3215290.5060170.1419520.8538850.07947980.9723530.9400980.8032240.2685730.04245550.5215690.8004610.7546570.4257080.5992220.2691550.2154850.4721680.7249380.6419740.5401330.6210960.196680.2161180.6846020.3937180.04845890.2009750.1842810.2191130.5699710.2855690.4943690.239640.6560650.1609370.2448280.03154070.4559940.07669760.1732270.08909420.2142780.8712440.7710680.1673090.39510.7056540.2526560.3185990.3593130.5176660.1825980.6528260.3494680.5242330.5292590.9124170.2866390.3454330.3100470.2689930.9764790.6048060.08306090.7214680.3981580.3423340.5633160.1346090.7056130.04707920.5037920.6777490.8837910.5423320.7775190.9362340.04886690.8151210.5770830.6501140.444012
910.6741840.6105250.03363660.1035720.6995660.5699190.817080.4728020.1548680.9878150.6411270.1523270.9385190.7001390.108880.04432340.9135240.3250150.6599550.4108030.2291840.798510.6107710.7526340.04286190.01266490.4364750.4032690.1582770.5767120.8923320.5065540.6175170.4293240.6475720.4188770.7197990.2792970.5938840.08543840.9180120.1696650.9999820.3398260.7426430.9669560.4283030.3257240.947150.5018010.1785130.05724950.9379160.1811940.3860660.9136840.5173970.4616740.9687880.1567730.4932280.8957580.4104320.5131970.6277130.9206970.197030.09269470.9608230.1980760.8427150.1078220.2583530.5338710.3790210.8804340.4028270.3421210.5745130.1853740.8874690.3611920.3208520.6515460.9318650.08919650.9269680.6626870.0130160.8025050.7093980.9832350.02112480.6769760.8567020.5912830.01158560.3077690.7008520.5012
920.8356160.9983820.1048480.364450.4503150.9751690.5751830.2584180.1374420.6671050.9029070.9091410.1906070.8509990.6150090.5263930.257350.3838680.5495410.3447690.0728960.8233220.255840.08346190.935780.8886840.2536770.6647030.924930.7424020.2508960.7977270.6081440.7134290.8291790.8250520.0245360.02477840.1512710.5084780.01100640.9751370.7768290.9287960.1837540.05058760.5489930.4253980.3584490.5000080.8285920.6543330.946080.2149490.1961460.5313550.4363530.3671070.5789130.002831580.4509540.237780.05876740.3020870.1852790.4240040.5257750.889150.8075260.02600920.6597360.7129010.8275480.8126020.9591230.3882280.6208350.0885810.5500740.2321180.4784750.378430.6852090.4187810.6822310.3464990.2461830.5999770.7931330.2244120.8778240.8130680.09099490.1401720.523550.1801140.9110970.5627990.6870070.0721533
930.8854790.5294870.09712590.1133690.5693420.6979970.501010.07481640.8824060.8285750.6128770.8401720.1448310.1751150.3387950.3187150.12380.4533030.148450.3667560.6510260.4610130.1139250.119290.6805580.08411260.7121880.8774080.4209060.100110.07131990.668080.08190430.1957480.9113470.2068040.3592340.3151050.03864240.7015230.5006240.4634620.7157680.1802860.4251920.2412020.6785380.7209390.9677480.2689770.09358190.4623810.05468870.7557190.5861830.6490050.1179230.08845010.2428210.0857770.7996590.4067210.9376990.8441780.9375520.7710880.7822750.5813190.8457770.180830.6428420.4994070.2749910.2640260.7140620.9236540.3610750.3067250.6502260.6645540.2386520.05951340.4925990.9132730.7913410.6408260.4982750.7994630.8831860.02383310.754020.6597540.4080890.8073170.7335060.318710.6310960.1164290.344570.0163773
940.6736660.2594970.9418180.02339210.3402540.07275890.7490790.7320160.8665590.114750.947050.8325510.352490.07381230.2571210.2056990.269710.4942640.097180.1290420.4113090.1501550.3486540.8324830.9108880.5446280.795950.06599560.3101460.1737670.9537250.3783520.5450170.3555120.5587230.8002360.9772960.8103290.717180.8812530.5801580.1679650.02775620.7435270.4905540.5201970.07697860.4138390.3880870.07985410.583490.6708870.3538690.4135530.8566370.01946820.3231810.6478650.200990.5098580.2033360.8785810.9914160.4772280.2384360.4136240.2658180.4489490.1112380.3179240.8069120.1931430.4438580.7169630.00836120.06818840.1335740.5829290.02522710.7263450.1187320.0929530.7903910.7281780.3138050.271620.8576350.2758860.4304780.8618140.431110.6832610.1948590.2011490.9566830.7508750.8083740.4493590.5407150.907438
950.1060310.5464520.4567940.2101490.9099420.896630.8002610.5375910.1895010.8629660.003832450.3738670.4833060.818120.8429690.7171340.1571010.8272450.8821170.6877580.6770920.4612960.5873130.4982130.1034720.1278940.9674080.3878240.8156010.5188050.9717470.6927010.5683290.8815490.6344670.3949240.9145150.820920.608270.06966620.1214060.2151970.06331290.6048790.635820.8360880.3818870.172530.9901040.9642070.5794040.8124610.7010810.9455690.5039130.812190.327130.7318490.1185580.08107430.3134920.2634440.9603890.5825960.1051130.8422450.7766850.3181370.4761220.9590270.3351370.9022990.5452550.5358760.1590160.7936180.06989010.2251710.5183460.8177550.8574460.2814340.235040.6541620.9317590.5088320.9989120.6052740.6453740.1160210.9141730.04985910.4629060.08795480.3482380.04731550.9488810.05466830.3405730.0840407
960.6826960.837390.6203650.643240.9421360.5687810.3936410.5977890.01027910.8777060.01434030.1306920.9847050.838370.1009520.3015980.187750.4672140.3739670.07324470.8379180.5368530.5187930.2912480.7376030.1271710.9967180.7893630.5961320.7478560.125640.9582940.6019810.2253120.2561020.7882070.3900670.3904760.9877240.7482250.2812120.8402520.4556650.3303740.8600140.6956480.8622110.01754780.02851580.8867250.1256720.5132310.0323530.1348460.7436560.4511680.2513490.05087730.6909550.3298780.9862650.03463160.7048530.7557950.3854220.1295370.3306930.6072440.74270.2550370.9611340.9324170.968110.1265380.6172540.1085870.008039020.4565320.4124610.5537240.9520370.07582040.6861430.5324490.2353440.3192980.4312940.5728890.6548880.8598440.3588610.7708970.04994760.3425990.2388710.6463490.2430130.6714690.6938710.260668
970.8814560.1440630.8184920.5413270.652330.1477650.9868530.5620020.7206310.6516780.3354680.1483820.2842910.03272850.5827220.3905060.2670870.4186060.3555050.4690270.06548380.7670950.3039150.7820210.3823950.4632670.8103570.533350.7284170.6081750.1944250.6525530.08639260.2297250.9106270.5094950.8602690.6357590.4913960.4737110.07630190.283090.3389560.2959650.1709980.1165850.1406340.1383710.1610710.9624590.9659190.4119260.9794080.8491490.9333310.1447140.5037560.5348860.9808760.7995220.01854370.0977630.6295990.7974290.1672470.508880.8637550.8166270.2832330.3921550.6455170.4322420.5637420.1636960.7267870.5369270.6552170.09023430.314240.3748320.04235070.4329710.07998530.9776390.06611750.6446450.7633890.3712860.4323380.1836930.7884630.4680240.2170430.9114920.353070.9495780.8828990.3003420.9964330.30164
980.2969990.2560870.4949920.7323570.9418570.236770.9846940.1205790.6411450.8069810.4782610.1410220.6027720.3113040.5963240.8076680.9847850.6798130.7483460.3365920.3238420.1798150.03069350.1640820.4215970.4375390.7676740.8459620.5047780.9264790.7369560.5619560.04692080.4400650.8747450.5592810.2388830.5033090.5845590.4076250.480090.9630110.3597090.5043430.8496140.4905090.6821570.3903240.3377560.461960.7638680.2619920.7764860.5544420.2027350.1902280.2583510.9738130.6592940.5308830.06453030.2468670.607150.2960260.5512870.3512470.3741160.5347110.8366770.04391330.08643420.2880390.2251580.7364240.4765910.8951510.2627660.5304720.7998220.02023730.665420.4094350.7840450.5258180.4700670.3474780.2493130.2493790.3889990.8399070.4884950.4098090.8180450.1118880.862350.02773580.1188190.9958180.4728560.717495
990.05049480.6488480.3972390.5394790.6588350.6366990.7182880.6973010.4348220.6150190.569520.9564820.2168090.5921990.6496730.6285580.4002450.2338660.07130050.176480.9674730.2834660.1093760.1371780.3420440.6436530.5855150.715890.2151640.4494240.5921440.6878690.6290630.07422810.3606540.352750.8848120.4389070.07608750.6415590.5439940.9130180.7663080.1684630.3004820.4566990.9416950.7864140.6074720.7076070.4555180.3146570.5514220.4029130.106550.8353050.8639840.1663490.9165860.9184590.9512670.2636030.02149520.4798940.8297990.09547790.06097560.3837050.7559430.9101980.7726530.2725310.1958490.9733330.4212060.5036320.6125190.02363810.05929550.6600860.3427970.2084080.5615720.9218870.9432250.4562160.7939610.7991620.7711160.07774870.1558850.5985710.7775330.698730.04763340.2184410.4935370.9429710.8925320.662829
1000.5471850.7275420.2608240.2572360.8529650.1278840.1354090.5349020.1765880.1349530.5813530.5097590.5161390.4176620.2160270.5027410.3109710.1300710.4262240.6028620.03050120.9094930.4755220.2714560.9698660.5466220.2430080.5945980.2925590.5390880.04201930.6646160.5608250.5857370.6045680.8714660.7263530.8342670.2989350.5930790.9109530.04295460.08760480.2749380.006777840.1823090.6552880.4775340.893240.1099160.4891440.2126830.1913560.8723280.4914880.7459280.8518360.3867690.768360.435060.3731170.522050.8759920.04553570.7631910.3086530.01063160.4831110.9026930.08427850.636890.8026380.2064840.1183230.7184480.4432260.1838790.1394060.2339490.0357350.1966930.7267140.8629350.9871630.9932550.2469590.5728990.626410.9275310.4114370.09021670.2853590.5849070.5379890.9937030.6795250.1915440.1306130.0678180.83357

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       x1 ⋯
     │ Float64    Float64    Float64   Float64    Float64   Float64   Float64   Float64   Float64    Float64    Float64   Float64    Float64   Float64    Float64   Float64    Float64    Float64   Fl ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ 0.768729   0.825426   0.615371  0.161835   0.317881  0.761032  0.726478  0.374274  0.966558   0.102955   0.604528  0.505535   0.833856  0.511811   0.406854  0.40288    0.801396   0.605693  0. ⋯
  ⋮  │     ⋮          ⋮         ⋮          ⋮         ⋮         ⋮         ⋮         ⋮          ⋮          ⋮         ⋮          ⋮         ⋮          ⋮         ⋮          ⋮          ⋮         ⋮         ⋱
                                                                                                                                                                          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.4361340.4408870.2262270.6356410.109161
20.7140720.2530670.2463080.8835520.126137
30.7965710.5451830.4739370.9362350.811203
40.3177540.07257590.4218870.5840660.639382
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.4408870.2262270.635641
20.2530670.2463080.883552
30.5451830.4739370.936235
40.07257590.4218870.584066
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.4361340.4408870.2262270.635641
20.7140720.2530670.2463080.883552
30.7965710.5451830.4739370.936235
40.3177540.07257590.4218870.584066

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

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

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

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.4361340.440887
20.7140720.253067

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.1424730.1934480.354178
20.6934310.7476240.972169
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.1424730.1934480.354178
20.6934310.7476240.972169

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.1424730.1934480.354178
20.6934310.7476240.972169
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.