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
14123616523
23345359831
38678636431
46152744347
510182449581
642948191024
7513510141036
81210101217310
957899510796
1062248210276
114814534918
126191887871
132248498291
989725561071049
990102104310510104
9915922511688
9926932934218
9937111838483
99410586539935
99576981092828
996781151031067
997195103371091
99864321991032
9995661674422
100023824221033

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

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
14123616523
23345359831
38678636431
46152744347
510182449581

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
164321991032
25661674422
323824221033

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

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
14123616523
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
100023824221033

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.8683060.3805150.6579080.8096050.08188460.5044070.1004290.2803530.8373740.02787860.8727170.1601180.2576440.3165380.686890.08610480.8362180.9346530.9440530.3923360.1691550.706870.5523910.3751840.3531610.9703120.9336210.01570790.07285280.7003170.1700980.9906770.02681010.700140.8030220.03987840.5537130.3932420.4362240.6952850.9524170.8548440.4439860.4147170.4311750.4369510.00188460.5836490.325110.6947770.3537230.8724470.511350.05494630.06396350.8305180.633150.3427430.5057230.9937520.08473220.7962060.1809220.1839770.5426230.7550130.7509860.3299030.4635660.3502370.1621250.1045350.978420.6561670.07820720.8562280.027260.3642950.9898080.1984870.7201510.465570.5832850.1067510.3325450.6470770.08776890.1987380.3055780.8207740.6137910.2076030.2177250.09034720.4040080.1405590.9261370.1565210.8158880.177271
20.2551530.1397160.1186190.01353670.945980.08367830.3626260.1264530.1297160.3504210.9539870.2139250.2658030.3268170.5362370.1588390.8761930.2503710.9660750.0328810.614060.7236330.1891940.7564550.5140570.4785870.7087340.05105870.6138550.953970.1484510.8880.8175810.6152650.344520.2839920.6206310.2198820.6215190.4446080.8020190.9897820.4701750.4638310.2059310.1216770.2936060.81890.00987770.5516540.6164590.7207260.4949760.04209660.1726890.8018690.5202750.5313590.5212950.7648290.6070040.5894230.9149920.4820270.3816860.1357550.1087650.514210.5484220.6689910.3385080.7713760.5324740.4930470.7276910.3447680.2987330.5489990.2780840.4653090.273870.2379290.1538410.7695940.7965220.08614070.4922390.9119510.7306610.9294190.4367760.09746240.6289010.9215870.8806160.2398970.5324530.1828940.541080.189981
30.7530060.09157410.6237620.9916280.7996520.4798830.5805530.1464570.413990.01930830.1466960.7652760.9218180.2872140.8303890.6533590.4023260.4450980.1147240.9355680.9092810.4746640.5192410.4562510.4790620.6090570.8675850.5105570.3804570.8839440.903550.8352020.165760.0485210.9686870.4657480.1569850.5444910.1693670.3575410.7745050.5188580.451190.397770.04631660.0810.09377060.2514620.6505170.5777680.1674120.2475970.4496810.7329480.5390470.2693960.4042810.9222010.1105230.7074220.1040620.8878150.5025090.9999360.6591060.9602680.7627380.6889990.6685010.9640830.1431640.3805530.841230.3719660.4847790.4657560.6917590.4486780.6981530.0504420.5534260.9983340.06835160.587390.5843780.01372230.8455750.2300510.2051730.7464770.5732150.5581390.110260.1646910.2901420.4117740.8754860.9222250.576770.552365
40.3049590.5113110.1718860.4324660.4381480.1973510.1720340.06001940.8338790.8358360.9877250.4285610.6699390.1560350.5516920.3832340.02138680.6934460.5158310.01467930.7262040.858540.4313980.4598040.9851270.2634120.9820290.5567220.7766080.0004079780.4890130.6188560.4184440.4545050.09034490.7062680.0699150.5333020.8774810.1095020.1864720.2939920.848410.2938750.868440.5962850.7746190.3320790.587420.1660440.2593510.473940.06901310.1521480.003437490.5070960.9537840.275840.6700650.1206440.4714840.832240.6648580.427840.6634760.6674710.6342850.04951270.6527320.7412520.3827510.4334010.2845240.1870220.4749240.6464920.3711840.251620.2931450.7789640.7751560.7638640.1824480.5937740.2682570.3517530.992840.510450.1759550.6582680.4152420.7558220.4117570.6367330.3871810.4520650.4598870.6846630.119670.558536
50.7867840.7989340.4101960.415250.8182590.2674270.01634490.9707440.3438640.5501510.7652870.8064240.66290.2708740.1073360.4648960.8041520.6581070.1370720.3930070.5085240.01979040.542650.4460850.3398740.4619850.2087480.3952640.691340.1650360.6166920.9747950.05469580.7730680.4872010.408630.3926920.5608090.1361820.9497330.6501420.5297370.9211170.7486850.8717250.3241820.3144530.9735790.4692610.2224890.7651450.5598160.5264490.4026950.280010.2007640.5223010.01400260.5431050.6595290.3264290.9506030.08926840.1623390.659710.9720610.3333760.7815180.8062240.2600220.996920.1558320.7385580.611390.594640.9083040.9022330.1571610.6040140.5486850.1706410.4252510.6424870.3080070.8991220.325130.8484940.5107640.1189110.535130.512220.11240.8321550.324120.8029470.5696180.3548020.8142950.8698710.16904
60.4301830.2386790.1037210.2795130.4929350.2499330.3730610.7804160.4607240.6411720.7223280.2482220.2451940.2650750.6811980.9952850.4269130.7428740.07812390.4188480.9092790.8729310.3152650.2727880.977240.4468290.6416010.8014510.3246170.7073870.9991940.06541210.5181560.2604120.8587620.07160540.6435420.5134210.8660070.4900190.8378090.7601260.6014890.3140090.1475920.7041260.5332110.01632030.939580.1973030.2085330.3309070.7983660.7785820.3466870.08625120.8946220.02919090.7053360.931760.8376250.3089810.2622640.01702140.1387840.251220.8768570.8257490.01633480.09807360.880220.4236950.9535550.7818520.7026230.5373250.8229620.2289980.7646990.190360.7836070.6810010.3690430.8216030.04843520.2000620.3024670.2900970.1228130.5506070.2216890.004020490.2209360.7483710.6768920.5929310.4515760.9644210.9401020.482321
70.9965550.9718840.8832250.07783570.6136660.1515420.7235080.0567450.175890.2735540.9532940.506490.3312080.2112430.2522090.008328740.6225680.6960920.0867230.5085480.7775440.4104640.1729580.5408870.2339510.4994910.8517390.6420360.9702930.3531820.7107220.9437510.3980390.4737720.5014380.3490020.2491680.5750390.3723560.5569920.6455750.5667740.03892060.4781490.8789680.7642840.7442040.3152320.1666170.03022830.2137530.1705820.776020.5684940.6234330.3272950.9352160.6717580.2387770.8915940.3232320.962990.5316260.8791960.4784520.835010.6698280.04739690.6044240.405540.3691160.7064010.8729770.5511010.9559880.3465520.4419640.1770260.1708690.08417370.6492010.607620.7246180.3694590.8392380.894940.724330.2175930.05871480.796830.5866080.5071570.089130.9652650.8262720.6479530.06860440.9720890.6237790.852593
80.8169550.9119970.1278320.6943930.008748920.9239320.5267960.003431530.850910.0999810.6567860.5581210.9480250.6990860.5177910.2088520.03483190.4868520.9498360.03095560.7787310.6382260.4980610.2733190.8397280.8976410.6235710.6786360.2585040.7101250.1366220.1736310.4446280.6828080.9543460.8827310.7942430.2603790.7448220.4110580.3829650.4287020.3203160.5598530.6425880.2902480.7585240.1468160.9248880.1803720.1612060.3842620.3426970.4755710.2074160.4401660.7067470.111210.396970.9456480.8808530.447480.3816680.5918150.9132830.2215280.7580990.9686780.5320340.5383820.1771690.3814060.06001270.8762670.8298660.01142190.3947130.06022860.09563920.3875650.1481220.4448020.9484920.9990860.3303490.8517490.6094270.5864860.2916360.7608870.1402550.1213680.1223030.9126960.8306930.168750.2025630.151540.1437650.323015
90.8101870.3795920.26980.045480.5916130.4055830.05586490.9239440.9366050.4712440.8824720.7996440.5671060.4356970.4373320.5748110.6940380.3714560.4437890.7666750.9907270.09137790.8751530.5872630.2407980.5780130.4547980.8275470.2448090.4264930.951720.7225220.1320970.1809180.2990120.7265220.9448070.8766540.02990320.967550.894270.2144390.6756130.9987910.3967960.6187520.9355470.8989810.8763980.9168830.4153470.8421880.1273380.5395450.05403550.648790.001609290.3569030.4935960.3601670.7015980.7883980.2559280.8693820.04582540.1926620.09725010.9368980.4985570.0517030.318470.7111390.7296810.06753890.1589320.962240.04213020.7692980.9744940.7057150.6101430.4803110.4590690.9384220.06306780.2395890.3203630.8732490.2013350.1902930.3187740.2678970.3614330.4581510.8718080.2674940.1775010.1046310.6180150.0424579
100.4017280.2503980.01565420.6797970.2170920.9553730.5377620.05608640.7760530.4843570.2720440.4551360.4447820.9185560.6752490.3241970.8818740.7999550.9398520.2867280.3472350.7557570.05509330.3833490.4739290.2879720.6107420.6584110.1422780.08605390.2439210.7411860.5166690.26890.9437470.4787140.1341430.2857580.6400380.08397870.3706580.4066990.6370270.4663520.520880.6979910.2917430.4968770.1038710.9738590.06772660.4109210.1682040.9544260.2094320.6310320.3716660.1773930.09429610.8377430.09322370.1439870.1117930.9632880.4125590.3439040.146890.4163570.6241950.1441350.2909670.2093540.7633280.940260.1994480.3646410.5723240.6853120.9367290.09104230.503480.7219290.7656570.5428420.9734250.03325490.4605040.2145840.3271780.1657330.0825530.6139280.1962820.290420.2920250.9676530.4372910.3989210.6560210.629152
110.9617380.305620.02861170.1762450.7167380.5636130.5479980.5636870.9768990.3693080.5493340.5639360.529170.4137960.2627370.3581930.4597020.3364880.1413390.6462740.4370360.1510230.2234890.2443450.008653240.4535730.675870.822150.3247180.6150890.5225750.228050.7131650.4503510.343740.7134960.9808460.01143360.8939860.2678440.4028510.6088980.8312690.04910840.1275820.5963240.9734120.6949190.1258340.4805340.8488490.8799910.3497030.06239120.6337940.1141350.8414880.9107460.8130940.95020.05976210.2303780.9195470.5738920.1869630.7471650.487490.4510250.8748440.6275920.1115770.5276530.1564990.1710930.2552540.2373720.7715440.1275230.5391730.2472280.6417390.7968460.1519590.250290.4297820.7222810.8703530.5967960.2908190.765450.0489270.7512370.6361110.6871010.3656750.5132850.1946160.4925090.9776920.987123
120.01782740.493580.844860.4472950.4329640.9524840.1218840.4607080.6552380.1699940.1052360.171930.6566450.5242680.9972960.7852430.8712040.8232560.3934090.3386950.9740090.5495180.6192060.7699090.2336890.9243170.02362910.1856840.1595030.7894970.6382110.6473430.2817810.04299430.6146030.5509310.005564960.8529990.4357960.928050.2225290.8358090.7503610.5564880.1185930.4472670.6190320.2999860.5512960.8047480.9258940.6270060.4180060.009643230.5951860.6079620.2602720.12990.645250.1595830.0515170.6805590.7912620.5747920.2777980.7494540.7892650.0273980.142510.3878860.8806920.2444880.8417220.5862850.997330.1512490.5659440.764870.7646910.9575210.5615050.7224310.4070340.1202520.119750.5099660.6040880.5634740.3917590.973370.9519840.02267470.8175010.03350380.2250890.09646950.2798440.5899960.4380490.506583
130.9201430.7868830.6737090.490050.8659040.8605970.929870.5751650.3227660.5813530.1828530.3923550.2153590.670580.5804710.4246850.6193390.6704430.2816960.736210.1321730.465670.7490570.5045290.5708210.453160.2222150.8069470.5055480.4619940.5210160.6213190.5203550.6000360.1579870.4553910.5759240.7299290.2988370.4008860.6510780.7648830.07106040.7405650.5314660.7252190.0469190.8968570.09233520.02866780.3103550.6326090.6434560.5093070.7771720.3040450.8419060.7967750.2382090.3232480.8474980.8729270.3906360.8480550.2810890.1434450.1246940.6612180.8537160.2279960.6732340.219070.1891380.3238110.2899830.2805070.8116960.6631770.5953130.952710.4009550.3691720.7673910.8679650.4738890.1844520.9706890.6480660.5044880.6765690.9701550.6884460.6497540.07533870.04388460.5085010.1294740.8497120.8838230.417718
890.2693110.9044510.5964990.3748050.9259150.6023370.4589190.3470640.01185130.900560.6849950.2847090.1086330.3680480.3415330.820450.1329170.1650590.479530.7819770.3721050.03908390.4520680.3913070.5751410.5327590.8014790.9089880.2092280.3525390.4306430.5121190.7824440.6605280.7325590.05878290.1181650.3188990.2105670.5221310.5169210.6277480.1494790.3297120.238960.2741870.1148550.6588740.04006870.640660.7699010.3061550.4865550.6913560.7639350.5353410.8648840.04768580.5173190.3457290.7096270.8937460.2613170.1775550.4898280.8913310.2809040.9102050.4705790.2960670.6633210.05227440.8491650.1770880.2909270.7615630.7963460.7686350.6938070.3296140.2224880.7508790.2096870.402390.3188410.3822860.6768960.434540.2820280.6325660.2205680.2901920.5287090.1065930.5837760.1224620.5950780.6785280.4668640.200539
900.6115640.421690.6586940.5643220.7915170.9011510.07565450.1356410.8605260.5559310.2166330.6911680.6421840.4146920.2251430.9236740.1066930.5136140.08699030.4512690.2383190.01838320.7795720.6785980.2691250.7277940.04266940.3110460.06456740.2479890.1609690.5050460.05325240.09008430.1060850.5946470.6902530.4888040.4154140.1944110.2710710.06254190.1881330.7916380.9030160.3087330.3395220.5591640.3129160.4042730.310030.09938180.5913330.9741790.7480610.8283280.1264070.196990.405250.6435350.7420690.1728950.2844710.291810.7512780.1430420.2923250.9003040.1165980.3216270.0418560.7512130.1400460.879490.3394530.7042270.5631860.2943490.6723220.06139650.7227240.5959440.3824710.8696790.2453790.03021240.9015620.04800330.7439030.6818830.1780910.8790180.9601790.4115990.7482370.1273130.8338570.02045330.3736070.689869
910.3755060.3629210.8112090.7198320.2117680.7341790.6391970.3939770.1681190.74130.470550.9226070.2208880.973180.4128470.2229910.6914790.5812410.2625560.9222280.2251440.2591490.4609310.2568360.7509160.1665120.029250.3156860.7872710.8652280.8360460.638990.9714960.01938640.5501210.06060810.2565860.9383290.3416380.7833750.8140860.7400630.9740450.7794480.7750080.7983520.3858560.7928480.768230.7504720.04875120.9388770.725130.4289060.2418070.8861650.4336540.5190120.7642270.6243490.7912830.1249450.01979590.6970190.3447180.43950.2017120.6324120.6396180.2289210.8469980.5593570.9935750.5151770.6037540.1056910.8071340.2559130.5780560.4956670.8889120.2736270.7018440.4986170.06471880.03481150.8738960.3675980.9280370.501710.5199170.2089780.8846010.07281060.2780920.2849350.9374430.7811080.8621160.701986
920.7623340.2200670.3470230.9574940.228120.9994670.9731350.2444650.01014840.2300820.1090620.654860.02648350.9041450.8046420.4651010.6882070.1196050.8928020.9177130.2250910.671440.226180.6547580.504220.4761940.182830.9456990.9394160.6136480.2937710.8581450.1291070.6083750.3622870.1726770.6664230.4953630.9044660.2676650.2549970.04439950.04463470.3454070.1384580.6494830.9920110.6631190.7480080.5518110.7639920.2101810.5792230.813320.348360.9224450.122470.4907950.4243020.1207660.8929660.9221360.4332960.9444650.7942330.06233280.3872540.9582870.327410.3441780.3044940.9897040.3568560.702070.6268740.4665450.8629640.7912860.1932420.7546170.8716220.8461280.6408630.7628470.8154790.3473270.1912460.5509860.8696870.1649580.5769340.01993070.4226890.7768890.5277710.8423170.459790.6442280.002741020.150896
930.1363450.888370.8243290.2192350.9975920.07729660.9686010.1694730.330290.4575370.8741040.6384150.9554320.2620670.7932090.05959820.09013740.6516680.9693580.004899930.07545050.8969870.5112160.4754770.5035820.08608280.05475530.4612340.1819540.07267930.1935590.773150.3934420.6641860.6598510.1506730.7055290.1053870.405370.7407220.03329930.6998130.6902860.2268040.5060320.4602550.8546920.2078630.3347070.002715850.8360040.3882260.2380780.7006550.923840.4077870.1521550.3785380.1439060.6212030.2689950.8348330.6326860.358080.6560380.456340.4421430.7274970.3163820.9462190.8429940.1314310.2271560.3059450.1239140.5147280.6416220.8748250.3844570.9976070.6152830.5531930.3608290.8876210.286540.556510.0590580.6416220.4590270.2113480.9904840.8714460.4505930.9672170.138950.2279650.3594360.3880760.7089260.453114
940.01581560.6338320.7818350.680290.03294450.7367260.8725940.0916630.4741040.3317480.04498750.5934580.8116620.6233030.6571770.7409640.5953190.5165880.1026490.1568360.5153850.295780.4622420.1559460.4681960.9121840.08451460.009259770.6996440.07344570.2762040.268710.5388880.993630.4804390.8329780.9598090.9760930.4532680.9733310.08239450.3790210.9742930.269080.1693910.3127810.5455320.6413160.7020610.6597410.541360.6910070.3613490.1571170.2174130.3332970.6852570.1747080.9157180.3631460.6289530.4742670.6999030.9525680.08201670.9001850.04295310.2128270.6280630.7466220.1183730.5396780.7480680.9612090.09801940.3719060.9725890.8707460.178950.3525110.2463840.1856580.2406530.2663550.1414230.4548660.9778670.9299050.3162670.8283650.3861960.3842080.3747940.05085510.3412360.2134310.1341290.2120280.6673220.324541
950.9453590.6999730.6227690.5400210.2362690.07516010.5422480.02533010.8039040.2513830.7334010.1375310.1430210.421970.7360550.5542660.8649420.209040.6289130.5366320.5207050.2931350.3536350.73470.6262260.9680740.5462540.03878360.5299440.6040070.5936210.8171040.2478050.07321890.2929520.8470920.4750160.9949940.0918480.3584710.4895860.7147690.5622730.0006269190.5280730.4912350.5519170.9389660.7017340.4529230.1562060.9913420.4437480.1513520.8148410.8302610.7724590.5340160.672150.07961340.8197050.7577170.5955760.4037220.2344290.4648780.2668340.8432960.5355990.2027820.8992150.329320.6683280.2129150.5107840.3172060.04122030.9188960.6289340.613680.1610250.5715290.7644520.4344560.4895040.6033620.1270360.584970.3427940.6868050.5504710.7805960.8425150.8411790.06675280.2880470.7992880.4955060.4257550.392719
960.02299290.2809510.6073850.7814110.1115740.1431780.5095350.9223950.1533320.7381590.8038960.620970.7789970.5567040.5447140.8140130.7615320.6164060.2837120.2992660.8683180.350260.3899760.1369070.5733040.3180630.4544610.7389950.4907520.2999840.7328430.7219440.2539160.6848640.2169620.856230.2883520.8887690.4797090.7873010.6929280.9321720.8192430.7942530.2574250.1061840.947060.1396970.7722220.416080.9955890.2897560.3242920.007921330.0570810.3159550.4158680.3296610.4795330.1474090.378620.4570780.9311770.4083070.209030.5839680.7618920.1855350.03627610.1409980.346480.9005250.8561970.2944710.07276370.6434280.02730250.1417920.09244650.2880610.3015810.6755790.3172630.7594920.4877820.6120950.3775710.003213620.6068090.4333750.8129610.3276010.6149180.3188150.2714260.6313250.7495590.7729840.4870520.517475
970.4331310.5435540.77260.7941460.2271710.7460170.6509680.8256160.1602580.908060.990920.8155670.9469530.5263140.139870.2607150.3799140.4991960.6026480.6977370.5619490.1610730.39240.04893930.4873690.7822830.189220.08916780.8824430.4571210.4689550.5953060.5735010.2688530.9491760.6164350.7344010.3754820.7612970.4604810.7803220.9163410.9154490.8826160.8311010.6871530.8093920.4167710.8423430.2933870.09217810.7813650.9536420.3405950.5543250.9580020.8318850.4878150.1794480.9244430.0386470.5182150.8718320.2381390.3862710.1608260.1566810.7406550.1806630.5643120.2784530.7585230.793020.5120350.7265390.7759160.5461570.8507930.7667920.5992280.01651840.15810.02805360.2544120.3047720.978990.1216830.4261450.02981110.8382770.9773990.5302280.7905030.925850.8564290.7481440.5725170.9180330.1430580.702311
980.8696190.05559490.03184860.7091460.630830.5924440.8109940.03033890.8036510.919320.3434480.9480450.9628330.9601670.5189050.662930.6334290.6618320.2832230.5883550.0947030.8821570.04981340.3019570.8184550.1113470.01817570.5415360.07631820.3003990.0566120.9831550.8283460.7377770.6258910.306950.01667270.6749930.8077810.8207130.2208950.7845290.9647370.6642620.9049680.9081530.7411520.6111670.8542520.7597310.7372870.4999290.4017760.02620780.33220.6434320.9599090.6051120.7303660.09939010.6495260.1122710.06104740.6072620.3227340.3946040.4377160.2523840.3991610.9103590.317120.7472110.6684960.05643970.814460.9102050.7114410.9394910.9302470.2919380.04882130.7104050.2886980.5403490.3151110.7799680.8061550.1626730.5998590.8118990.8493650.4629970.6318020.733250.3914210.7466090.256950.2593090.3160080.927874
990.133050.1351360.265520.9564480.1995670.9063430.5693360.5928270.03796730.8319070.1425650.7569680.1735210.1187790.5688620.04370330.8945340.3375840.4372530.4085340.3622620.7372330.8692810.6447310.2080850.1307260.1596550.07124570.807990.3006580.07943870.6586020.7846280.05583770.8570130.442510.9482630.5083290.7195860.5589190.2533050.1530160.2557480.8512150.06699830.283140.289940.3066530.6621550.003590910.222320.244290.5640360.3474040.6672930.8079690.1989470.7585810.8710460.9636820.6192510.1455090.2389220.175810.9530740.3741890.2420290.3384710.4839780.3651520.05325960.1017520.7054380.6894810.02043760.8780130.6431570.969580.8639470.3636790.2512420.4101930.3681990.7448240.6167050.04952840.2246240.5066340.9194390.5530340.1737740.8346690.1233120.8526230.2754140.3533860.9199290.4337470.8281760.226691
1000.5582990.9012190.7638490.4173150.7389410.4233580.2284830.4186350.2567820.9649930.05878690.8278050.2535930.5847410.06146190.6328760.8820850.1153760.0462880.2763320.7153550.9494770.7354780.851740.5643710.2913160.4758090.4332640.9261440.5130910.6230260.7791110.8683750.9805660.5884990.2541050.2581020.2667250.8768680.9506170.9828820.677310.2518950.8913190.408980.7790170.9801260.423330.5744150.8475640.4660230.560340.8857080.2944940.2679840.9838160.09775620.2555690.3551420.9945390.853510.837450.07499920.8555170.1237010.7394520.5013530.5797690.3305810.1957150.1966980.4417140.7432730.7541560.0230310.4963280.03259960.4423870.2339770.5501640.1514970.4175250.9023710.56590.6442140.1686480.7658260.8725520.7367250.2791240.603920.323340.8958740.3744630.8556340.8186210.5880760.4870430.6696010.473727

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.868306  0.380515   0.657908   0.809605   0.0818846  0.504407   0.100429  0.280353   0.837374   0.0278786  0.872717   0.160118  0.257644  0.316538  0.68689    0.0861048  0.836218   0.934653  ⋯
  ⋮  │    ⋮          ⋮          ⋮          ⋮          ⋮          ⋮         ⋮          ⋮          ⋮          ⋮          ⋮         ⋮         ⋮         ⋮          ⋮          ⋮          ⋮         ⋮      ⋱
                                                                                                                                                                          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.828310.1817920.6610180.9565170.179727
20.5217710.3848060.5704050.5403280.545185
30.699880.09340560.7307750.04642710.307018
40.5537440.964430.8840030.3168230.593356
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.1817920.6610180.956517
20.3848060.5704050.540328
30.09340560.7307750.0464271
40.964430.8840030.316823
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.828310.1817920.6610180.956517
20.5217710.3848060.5704050.540328
30.699880.09340560.7307750.0464271
40.5537440.964430.8840030.316823

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

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

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

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.828310.181792
20.5217710.384806

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.5619170.1162020.888651
20.827270.636330.381904
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.5619170.1162020.888651
20.827270.636330.381904

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.5619170.1162020.888651
20.827270.636330.381904
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.