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
1485610767104
23289461498
3376101017281
48852316865
591094278788
674107639664
7172584641010
88106143101073
939641210429
10597610310465
1197269107491
121811736986
134328777711
98910216622662
990198915101025
991457104281027
9923825910101051
9936107210121074
994783108988610
9955631010643210
996108593757101
9979761772891
9982682841728
999610732581073
1000910856210667

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

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1485610767104
23289461498
3376101017281
48852316865
591094278788

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
12682841728
2610732581073
3910856210667

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

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1485610767104
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1000910856210667

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.8607560.5969430.1674630.2203520.6341710.4315840.8574810.97920.5229180.9534250.8781030.9719490.5942340.6481880.7483630.3337010.280560.8668480.6912890.1958530.3242870.4426470.7447010.9784260.6564390.03059040.3037580.8508620.6380450.9906650.8847570.9951910.502540.6251430.7766380.4520640.8493640.08791230.9476840.4320630.9188970.5007580.7329780.3161480.278270.302470.7259580.1137250.1339830.06254570.2049890.2607550.8363910.3376660.04148970.3402290.03690330.2077430.5174830.03718310.7123950.2088220.3940240.2829730.1179190.1129940.3019290.7712220.5141470.697770.8635880.4304540.6044420.1918870.1011880.3266660.3751490.3336610.7912370.9554970.155830.3208810.4101610.3904540.5222470.1271420.9801290.02169470.04820840.2983520.7288830.1449120.903790.4602340.8723590.06336820.07618830.5506180.5865350.320268
20.1943530.511160.6275610.03815350.8842290.008882910.7620410.7975730.6750720.5366470.9555280.6159660.5163370.5266320.3031260.1313120.4185550.5487170.5566670.5725240.2824040.8734220.8650240.2257860.9301810.7588390.2345280.5025970.3863030.07530630.05831880.06599790.6887770.7456120.08973780.03211410.8164390.5184140.8331460.5061140.1492880.1327610.7344690.7380050.1753840.07045080.5444530.5442670.02805580.2106610.9168290.09834770.4599590.2115480.1250750.4072970.4338410.09158530.6484610.06146270.1637880.6745410.2503810.4292290.3218860.1305250.4690460.3581140.244640.1535980.1929830.244610.4766460.8590930.1608040.9243720.408670.2445880.2087160.9832740.4248450.5575730.894330.497850.8706430.2259240.9281110.9596730.5426280.780350.1295180.7189370.6199430.883780.4919880.05426020.09718150.3408380.4579440.344453
30.9173270.56790.5278550.02451910.325040.9707010.6344490.7668470.7321610.5467050.3605320.9293860.6915090.4177870.490960.3736930.02666760.8513420.91590.6375970.908250.5577770.4898840.3307020.1918210.8699190.638440.691760.4747410.9408140.1577370.3747290.460840.8389470.9841120.3447360.798410.5193010.6026930.01791690.3599020.6991550.8796670.2868460.07440110.2056060.3056260.9768250.301870.7788940.02486380.9709530.02424350.6364010.9722240.5095260.9116950.5696450.6133120.5165560.3079340.693950.7556840.3422880.5199140.7603660.3243780.7352210.479850.01108460.2996150.2561640.6425790.7209520.6713550.002911880.01629350.08631260.02722530.005724820.6034870.7986080.9968720.2383410.8632320.9024790.6810450.7791120.8860370.2933130.9080990.1221470.4668170.1321210.9414040.9527590.166310.572230.08212370.890252
40.8832610.4599850.3157750.5996370.3499330.8090550.08200330.7549770.06175030.3871950.4249850.3316540.7153130.8800340.946740.04048550.02711930.4948950.6766970.6723080.2150780.2572250.110130.7301340.4221290.895990.1032710.009977260.3770990.9992320.8171620.9544050.1301720.7665860.3009520.4489490.02780820.1935380.5034490.03120720.6735540.1136080.9252670.819210.1488880.3249210.4102140.447850.5975910.1760850.9800060.5661340.4250160.8137080.3381030.3839140.6059930.1236130.4936820.3407820.587160.5913780.8304380.6582440.3489860.1624590.5902660.3164270.5232520.7706870.2833520.1817080.7206720.8798520.1785810.7657860.2637520.3381170.07958750.7915150.7224820.4517810.4296720.7296440.6494490.8800570.9623520.6353680.1990680.0004487520.2274210.9713430.02493510.2556350.2626480.3068430.5703810.8364550.507280.683514
50.65470.01609170.4592850.81980.2975110.4228960.2067030.7454080.8457760.4339160.3616180.3924270.9353140.8339410.1588170.764080.7563010.06691920.2353980.369090.4769580.1691410.7869130.5672390.1133670.1032670.4190780.5497050.1304650.889790.5717110.4348080.5304570.4859710.3461040.03874550.3415810.3296310.5784320.1360640.01411740.1418270.7305610.2819630.6452750.07023380.1868140.5928560.7019280.5109950.8798780.09855790.185090.03268470.2724810.08386740.1444160.5859090.5363270.2763250.3559470.7700860.5825390.1239210.5781170.9750680.2843170.4194690.835990.3047080.3452530.8617240.5235730.7936070.8534940.1849950.4519450.3854090.7466570.7392250.4952240.344880.106730.9617060.2928940.3981730.9453280.7170290.4320790.627840.007276330.8068990.7173080.9012960.8189110.1519310.4640190.877970.9367850.302482
60.7391870.4255610.1225940.9398790.2171540.06673950.5754310.8000260.761950.6612040.3076210.8151210.08772940.9496280.7322730.002155690.8613290.9306460.8057740.6786830.502240.06919620.6429170.03878190.771390.1788480.8815640.0004920340.5091880.1327140.7221060.7037370.5164190.3381830.8954880.757870.7460410.133330.7626480.7490470.8265620.4218940.7495180.1645490.1231460.938140.6136830.5413540.5098610.850020.4857130.9715510.2162790.8634820.1969490.2071860.8993910.3319180.1162750.6181350.9630.4789580.664860.08718720.7336610.953420.2002510.6384280.4013710.2342090.3654850.233020.4888470.09944820.3112540.8464580.8909920.7339280.06242760.7703880.07570950.4830420.09847730.8853420.9699460.3775720.02664120.6735710.18320.3858940.1038440.5208080.9647640.6141530.656150.05403210.8495980.5841750.8141830.0564616
70.4267050.5292710.6835080.3042220.9741640.3230550.8512590.7104570.6021060.91310.1322520.593710.6693270.8343080.6039870.656980.1553740.5880570.6832860.3657570.1344850.5192340.9796540.04932870.7440470.8951780.7478940.2576060.08477170.8621980.8137310.6895360.4656970.4080420.9188380.5987480.3210450.4273730.2344520.1380130.6253230.8291210.765320.7212590.7273030.1484110.546850.5221330.2815850.4386840.005944560.03487220.6213520.3979180.4584570.5558530.003312140.7998530.8615330.3741130.7364390.19790.6879060.2557270.9546250.5883450.9026360.2851150.6833320.2137980.9031790.2502150.4126680.2052370.4175060.7721030.8144120.6286910.263150.7068550.725740.02419320.1413920.2837870.1226370.2776870.8583550.7090230.4810570.369530.5704470.5593780.6695380.115530.007362410.233590.08975490.6084940.6269050.352263
80.2479340.9656950.6813370.7374750.4146810.8936460.689150.6204720.5364810.9216420.4638160.2015560.5081620.330890.2776090.7429640.7180280.1137640.3501790.8465340.1642150.6847150.3802320.1153170.130150.2221350.9705640.965140.7185470.278770.0316720.1976890.06598730.7797990.6572690.5025620.6540960.5724390.1650020.8169920.5434770.278640.8566920.5374420.4581970.1704610.1009680.5340010.06982610.6281480.7944890.8968220.2801310.0642020.7947510.3766480.7930750.04426540.1920530.4106120.1809960.2147580.8766020.6495620.1851790.9402610.6765290.4577890.03466210.7022650.3779710.0611420.4415490.6239760.8504530.2943490.7656880.4010210.6864570.2446310.6305980.08471670.1756240.1695280.1182070.2332370.06715450.4698480.8694320.6431790.3419870.5265880.9936260.2813270.2779510.4581580.5328150.1472220.2165360.799598
90.2646870.8380420.743580.6004280.1621720.1177670.9798310.7654520.9887310.09127070.08043350.4548780.3705910.7698130.7970590.6315030.4321920.1223270.3068980.2568930.313750.267890.1776530.1398590.2745420.03775280.7455810.2079750.5429840.48620.5005450.9096530.1426910.6053820.6831780.05560710.01213110.1396470.08164530.3563190.4565080.7765230.3132060.9750420.05408540.8984570.8369260.7662230.2440220.8845170.8942150.6789360.8511160.7007520.4520750.3048680.6673860.8371430.5831440.2233550.4012490.8668540.2789850.07122370.1801210.05239390.661890.2819460.05514060.4263010.01371710.5259660.2488760.07268610.5012360.7068730.9324270.5738440.415580.176120.7451130.8428380.6459950.6742380.1858250.2647380.06947920.01560690.3292540.9228260.7905520.6035750.5494630.5823950.1893590.826260.9408690.004728750.3202350.407996
100.1589460.5059140.6625270.2758810.04220680.7970840.08973540.1258680.03006460.1566140.6956710.06483730.5591440.09298910.5123630.00791970.1644630.5895750.4569510.7620420.8379920.5411940.3397520.6588140.671590.385070.4500150.2457980.7740160.6308470.6853880.05307670.7774670.1921720.6371280.4237920.8575790.154080.5444420.5179960.6481710.1514060.1832810.5643180.9220940.3960430.609360.9287250.9303810.2861810.5619850.2892330.2781720.5239290.3451990.463730.7641080.5563460.3478470.5191150.3759110.7137440.05702210.8064210.5953210.5196220.8047530.1469290.1556740.9377820.6418480.6740270.5430420.9612310.08881210.5181280.2819990.01369650.989790.8647530.8137280.5229190.9818640.7912990.5657750.1576960.5009340.5845640.4713340.09925530.0512110.4731830.1921350.8997770.7747390.7546350.4926580.343080.1153660.907372
110.05921450.8881870.3146860.6615170.20090.6837760.2333450.1846450.5199380.2599210.03265660.9679470.8629670.6399580.9796850.9761910.6327760.4561460.2874230.02031960.3840120.4813510.2684150.4554190.8683770.720980.4336810.134310.02886060.5594020.6011430.4376130.06776240.7524790.9211430.4105730.3214520.1477970.6337750.4690190.5126630.1972610.8713680.1653180.5151390.8014660.7296870.9717480.8343790.0770350.7772460.2141810.9702250.6239160.1312350.8369150.09312780.9545240.503610.6482040.8689890.4842190.1317470.8093530.07971350.7907310.208190.7765690.9627070.6638580.04131060.8638030.5850680.9055770.1162830.3436990.5283530.7698490.1626330.361440.02097510.451550.6415260.419550.1497290.5188960.3652860.9761020.8206650.7668510.4724880.6817550.02968240.09031640.4936420.671730.4056460.3175810.5068190.0915458
120.6819690.09002480.04097110.8953280.4387590.287910.6556370.9319280.2658580.1645080.8960630.8793950.1312960.5062380.8153890.8703360.5896830.2942320.2437780.3290170.8838350.97260.2542250.151410.7184980.5258380.5317370.2422540.1995370.8674160.7024330.05962820.05661450.329280.3482110.5624260.120460.6744860.6157330.3498780.8147760.9724330.9755060.8258660.6300350.6484520.2680420.9200660.07192180.4166930.4512080.775310.05584890.7641590.07380720.310130.8618730.5238930.383020.2189020.5373590.9798240.5945780.4346950.8858350.2129180.8223360.2555850.753290.8594740.3343310.6017680.2755890.3844290.2435260.486810.01408850.8286030.7607260.8016930.9406280.3882980.7501890.5137410.4204760.1813120.4622010.8753850.1768850.2229970.3932630.8058890.3282960.07680370.6007170.2822490.2222150.155460.0199810.677369
130.3159190.5807570.1112470.2403070.1079630.3669670.07179690.6171760.8284050.6732770.1458540.3616260.7008910.7835520.1407240.0549040.05014360.3327760.006136770.6688150.740520.1201520.8684160.5135590.1054110.7502620.4945230.4528880.1710460.683830.423730.4676140.7876370.07822340.8459730.2631840.4383840.1768580.4314140.6193160.05290050.9958860.2203250.3790120.1842060.8034160.01401270.1603980.6379580.9336370.3264410.4841870.4284960.556040.5917520.3503360.9500970.1044550.4757430.704060.7053630.0693320.449810.4647120.09641060.1312520.3502410.5011060.78720.8561920.8951120.8266640.2539880.4820090.1107630.2077420.4122630.6192720.4496130.1657290.9394550.9254270.08972910.2646530.7499450.4387250.7967830.168960.5204640.324160.674580.1204330.4405850.1232910.004380550.5558880.4660140.9054920.3716130.752003
890.5151050.7272960.7812060.2587480.3809140.1262660.3505950.5415150.872510.7591070.7593280.04003010.5350690.4447160.09004640.5867560.5352180.7904450.745440.1385810.7668350.4795090.9630980.2594220.4648720.312040.8032290.6309690.7772860.4191570.8640740.4115380.5779580.9327370.8666390.380840.7110990.1142760.09154090.6355080.3318190.7415320.958890.4664370.3683640.8303220.8711770.2022690.4215360.4291550.7098540.6225090.5726950.7701090.2511670.4257280.2918810.2546670.3250390.6457070.538340.2097690.5143350.672390.4272220.312230.7279060.5193970.369910.338170.7401840.2957330.1113910.1559410.1452120.5865980.7108360.02069990.9207540.08984830.1508730.2096620.0677730.5066640.9278640.7856120.4367490.4847780.9817520.6610650.2377980.976590.7924270.3387420.4111260.1144570.3670630.5205290.3511960.248254
900.4752250.9242460.919820.7182180.18510.290420.6117890.2631350.1512050.3258920.7813880.1505890.2318630.9826210.3926860.2789080.8431390.1374640.6417660.4649890.9364930.07027480.6704890.9175870.1930140.2414820.8371690.7816540.8372470.7832460.8403280.8586650.219210.4747390.6424690.01786950.7321160.09260750.6027260.2066490.05885880.7945790.4379220.1756330.5931410.7025890.4859330.03253420.7571110.6456130.1401360.9175260.932980.5298760.3343980.1904740.3959520.8497020.2601050.4322070.7168320.5041980.0005174440.1329610.35760.3545040.16680.5907320.201530.8853670.6851140.3833820.5388010.1276040.9999070.419380.4757090.4078180.08132890.3790410.410550.6070730.253230.814180.3915670.4057850.6069860.3546110.4168070.1016650.4291850.9822220.5757920.8681390.05890110.5014660.6966440.4316580.05241430.883497
910.3758530.620640.4716920.603230.8240420.8335080.9197470.4882880.2881850.9009780.3969680.2944550.512120.342870.9428980.7373990.07630540.5848910.8777470.1710430.430320.1731370.8295760.5689060.4912220.3200580.2760640.8262950.6154540.3336350.3175010.5769090.2045130.03839390.285820.3851220.05654410.2379850.1114530.9744140.7909680.4999910.07615690.214450.5658850.3659010.2586830.6872110.5706990.03015910.4432030.1984760.1955420.5325960.2074320.2287060.7684190.2673820.496710.4213430.04664160.423590.5383980.442650.4536450.7997370.3812720.7549210.4243480.5676750.9929750.2960190.3229940.9470090.946880.516020.9280340.1991720.7928430.5608770.8216870.670350.6069450.213520.981850.9132810.8853650.7074010.6892130.6072750.04576860.122140.3254710.5548380.9548220.3864620.7164240.359780.4591580.585317
920.3307660.2469460.6738520.3643520.1308460.3025750.5387460.6696610.3214050.01974740.3774520.8270690.1371040.8715080.2599960.8634120.1848960.6058020.4151090.7973850.2173270.727550.3314810.03916940.5627070.6372010.2911340.01951270.6654960.5018310.8521580.1810950.5409290.1442040.3150970.4885720.1686740.2797110.9736210.2331060.4987270.4122830.5400820.3378030.9281280.009377730.05868790.7621650.7641080.09402020.6249460.3747070.5666740.5870440.216570.6859370.7201710.09688470.9466830.3050530.796660.5464260.4494180.9373970.5237980.6089460.5142550.228240.3906330.5890440.3496780.08904850.4424510.1891980.5246680.3218420.1810150.4199040.5967240.5364740.2187040.1600010.1717510.07123940.4546730.5172650.7129490.8403690.2639560.007718510.8954720.5898540.8045280.2878730.6783640.9819990.2506980.7644830.5873220.820353
930.05172850.8308760.4538790.1726160.3345160.8011760.895090.479280.7148390.3558880.4542030.2815370.4608340.4972520.4898440.9678010.564330.587870.8745290.8798320.487910.03060370.9756740.7086630.2508710.3402450.07146040.1504610.02132840.03409860.1789470.4783950.72080.03686950.4235960.2243310.7472580.7688680.2645140.7165530.495210.7158550.6488840.8334890.7741390.6809510.5279520.5030620.1353080.03734550.5729320.7776090.06296190.7257850.8034350.2858030.6364370.3393660.8117030.114270.06036530.9516480.4689550.03131610.7706510.7512470.4089560.3923590.8981350.9612470.6556820.2899490.1412160.9403430.02501520.695550.2548880.1331170.1496970.4184660.2460890.6003830.6248960.1837470.5286060.3196120.6602570.7026430.58760.8222680.9863280.6285390.6001380.5446290.1892340.4837140.5896870.8798990.3743740.689105
940.7261540.1861920.412160.8377240.6995670.03612440.6328830.621560.5918230.710070.5641940.4033980.0557820.9718050.1526910.913540.5233430.3563140.4758290.5344540.2044630.4414760.3654790.3664810.4038550.7784020.198480.5442640.5999080.5145650.5511550.3814610.1452460.5800270.5641860.2306280.2849840.7680740.1754940.1251950.5667970.5028180.5676820.6791720.9182370.3765270.7897540.7100910.3507810.4308650.6380270.1642120.5156460.9965170.5489160.2169520.7102660.1376890.6506080.03613110.043060.9913680.4243410.5204640.2214020.1043450.6270320.7762620.09960490.6995340.6428560.7058480.03195060.8539370.6418750.7138710.08191830.1512650.2245170.779070.9835070.3476620.2290970.06343970.9472770.4521340.410170.4154850.9078260.3829530.6662330.464570.7605870.4711730.03236540.1082210.1775470.1201090.4815770.573587
950.4454190.4354930.6551970.474060.8182680.8483640.6760030.9988480.4913180.4166330.4770710.7252250.02434430.4427270.9691040.02718340.1450120.2530720.732230.5496750.5649050.5268420.9637140.5848770.7598420.4427070.8843410.06460720.7088540.62930.6786650.02086470.1065710.339210.1432830.6939450.3493220.6883590.492480.9331010.8212350.1293250.06120720.7200090.7320820.2282420.6574390.9528550.9067180.195210.2401750.3451440.4901450.1351880.5375350.820370.4703560.6496310.1092210.2455290.39610.3825730.4215050.9639660.806010.108010.3182360.6498960.6873630.0869520.627310.5988030.02689980.4742040.5034510.7455860.5331410.4020630.7397610.2301860.007658260.3205540.6019570.4730120.3679380.6745630.9312840.2599740.4131150.3414610.7001430.8328730.9002570.06761510.2281240.5900350.9516020.4052810.5262270.0449277
960.2678670.5512130.09315110.7060770.83710.01967540.8765950.7290130.7872920.2298960.1765160.3451910.9491850.9252140.596720.5500580.08289040.1130780.529790.803720.7831330.04534360.4113380.1947830.199280.05447540.3038320.6076520.5133810.03992960.06023030.166710.320130.9544380.03251210.4060840.9984970.8777380.2523350.6304430.4734940.1454980.9315480.1987280.3945160.8338410.3943980.1778790.9545050.9892120.2785580.2113510.6212360.9728040.9903790.3632760.9420720.691180.5221870.2727370.7822270.2127750.5570250.9814560.3411740.8975090.3220160.4213270.8549380.5687520.2706940.9364150.206830.2367160.6786520.4670350.1185980.9336220.1275930.3342370.4996980.05490420.5743140.4486430.8980930.2218310.3570890.1862620.2677870.2384060.6569150.7798110.5020880.417990.5350440.949960.2202490.3249460.314370.251166
970.6176210.4557560.4783460.5117490.9908720.7134150.4221440.3781780.9545750.8240080.1230140.9933890.2006830.7211320.1784860.7436650.6917340.8815670.9430040.1209110.9986960.06251740.8408610.1088840.4688870.9468650.0310390.9743950.747280.7906210.2584450.4411280.5368510.7701020.2793970.5622250.4270640.740520.814110.4895760.8330150.4047160.9221340.583860.1081380.7965080.1627160.163780.3349210.991780.2825050.5662990.6831230.6344970.3422840.2470970.6671510.3399110.3662320.2116220.3203870.9631570.389540.9105990.9321050.3016840.7208730.6440230.2305120.1594840.8839150.5110530.5794160.01797660.1063590.9773510.6798450.0357160.8913430.5819770.3590780.8607480.9008860.2116150.3131450.1670560.2688830.9582920.01049510.6481560.06801830.8899730.6192940.2454410.3456710.708090.3500140.6220090.7325960.357318
980.5436630.76380.6854970.1453590.6329680.7879610.06760710.03551660.8857490.1107820.5155870.8813460.8415630.9186130.8398890.7953080.3407060.2249810.768370.5440180.9712930.8747440.4735040.009478450.4491110.8125170.2007190.4718950.354630.5678520.925580.7655940.9043440.4360860.3742170.1497080.2623730.9924960.7647770.299740.8803940.1063530.2767060.6361410.8731630.7287070.5849280.4736290.0491510.07877790.5843390.8668660.9254290.5537480.6937760.09850670.02657640.9098640.3996950.8195280.3454040.9478650.2453730.3623870.5740960.6299510.7547750.06167480.8381930.121390.4229450.949650.1821150.7223750.8481260.6737460.3183530.6311490.8043470.4314080.5058110.8502840.2806340.3146450.311420.6859010.6264330.02374290.4358720.5315340.482420.8492740.7471420.2501690.3968680.7194250.3139470.7252020.2514430.233205
990.03742240.3201190.9358480.5615950.5530540.4763520.2893650.5857270.6706670.6796430.2727720.994640.5843550.281980.2592860.4256770.6932960.8100480.824740.5147620.9692450.8175320.3475690.5338160.08857910.8108040.4168010.02884560.9818590.408810.8836330.6808060.6174070.9045090.6459880.8472520.5592490.6729530.2207670.106790.799630.07955580.3035940.281070.289870.3735210.7888470.5266150.5029260.1021680.6741630.2971310.6611710.7078990.2410450.9263940.2305410.6911180.8900440.7269120.790360.9354150.7449170.7292860.281640.4199220.04326760.3731660.9443220.02168130.8141240.3455340.2330130.5552950.9603060.2604370.007379850.6647630.7121470.8806750.4252890.7950370.07325060.05033940.2957580.1927040.4729910.5555810.282790.7805620.4270550.1200350.03924230.05479090.01248530.8046620.7614470.4132180.5428950.836592
1000.1460750.2631880.1926650.04937880.2277930.0932780.4927250.6171710.8839390.1707540.02329680.3002210.06168960.2398790.9594280.8238320.4759170.5043330.7946890.8759540.1958590.990840.002088470.1163940.08993850.9563060.3074810.06764550.08324520.765240.6367030.9308360.9744890.9927890.6118840.9851370.25260.9367460.9461320.5627110.6471960.1255790.04025780.733740.02496760.6778450.3656830.9437530.02068640.08604960.02478550.876110.2434610.002461310.725720.6459340.8323160.07493030.733940.5083160.9063730.5407590.3612270.8561170.1350740.6067220.2678830.5202580.7532840.6069170.7993040.5373080.8097060.8973580.9423280.3801070.3271380.3893970.2788710.7265910.2333990.6885410.2965770.7031840.0506380.9200480.4772360.3196250.09767990.1968870.1340250.5266330.4436760.115690.6660730.06129820.1505290.06475930.8063350.249394

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       x19       x20 ⋯
     │ Float64   Float64   Float64   Float64   Float64   Float64   Float64   Float64  Float64   Float64   Float64   Float64   Float64   Float64   Float64   Float64   Float64  Float64   Float64   Flo ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ 0.860756  0.596943  0.167463  0.220352  0.634171  0.431584  0.857481   0.9792  0.522918  0.953425  0.878103  0.971949  0.594234  0.648188  0.748363  0.333701  0.28056  0.866848  0.691289  0.1 ⋯
  ⋮  │    ⋮         ⋮         ⋮         ⋮         ⋮         ⋮         ⋮         ⋮        ⋮         ⋮         ⋮         ⋮         ⋮         ⋮         ⋮         ⋮         ⋮        ⋮         ⋮          ⋱
                                                                                                                                                                          81 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.22990.3158120.9102570.4020610.120818
20.7982830.8602650.7652950.824350.512188
30.5326710.5676510.6716150.1154910.93814
40.3805940.8110250.5624310.6413340.89473
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.3158120.9102570.402061
20.8602650.7652950.82435
30.5676510.6716150.115491
40.8110250.5624310.641334
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.22990.3158120.9102570.402061
20.7982830.8602650.7652950.82435
30.5326710.5676510.6716150.115491
40.3805940.8110250.5624310.641334

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

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

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

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.22990.315812
20.7982830.860265

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.6091330.5026980.659026
20.4263640.0871670.887649
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.6091330.5026980.659026
20.4263640.0871670.887649

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.6091330.5026980.659026
20.4263640.0871670.887649
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.