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
1103910527188
2973997210410
310147546173
410614258464
52137844998
68666565879
7191053510928
872108551572
910372131225
1010958665155
1188181110119
12226108106871
13101594465310
98937746161099
99051066494282
9917986614271
9922644728537
99392510544924
9944399453364
99587910434178
99653351097128
9974577412818
99810557642135
9996476526511
1000101875571015

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

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1103910527188
2973997210410
310147546173
410614258464
52137844998

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
110557642135
26476526511
3101875571015

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

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1103910527188
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1000101875571015

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.6855390.2802850.957080.5476980.5789140.2175320.3835530.7416610.9918970.06321980.6871520.3497310.4693580.1470150.5487280.01291530.7865750.5810070.1632160.4351210.4314650.2037310.2744130.2106210.4628230.7256460.2865160.173570.9193480.8873530.6815480.05299910.392180.07784740.9783940.480640.5221240.9155410.0148590.619860.6616050.4437620.1575310.3642880.3181760.653340.00236660.767720.8169320.2059460.7936240.2094170.3316780.2529310.09636310.7244590.8761560.8865440.3987750.003995240.9484710.3937170.1035830.4524860.7655820.04582240.2522660.5938850.3191140.6393510.4702580.684280.1509120.03061310.7906270.5956020.5124760.4117670.1350280.548470.9527980.4240340.59590.8093940.03076890.4654760.2845460.8739650.08221370.8414090.05024510.6006950.2768350.3308420.6413350.2915160.2037320.7789970.1498940.0213861
20.230650.3560240.1024740.7771340.9101780.9918750.04874270.4199330.3702650.7246420.4208680.32150.2039610.1939460.5083530.7323440.7594030.6381390.4664490.4700010.1542530.5184540.5779970.4019380.9505230.4667890.3350370.896620.3954050.8474610.443590.04991290.8333650.2984420.9356750.6911440.6157380.8928380.599760.2372580.05949320.2317390.2466010.5781030.7147810.1327250.9664250.5425280.5796440.9509040.09415470.2033620.3384640.1348290.9582310.2248820.5591630.447790.4896620.06500160.2652450.6369390.5498780.85210.5144270.7170130.6531590.0558230.5070380.1263090.7909370.001657420.06467470.6654530.9239070.3694810.4168660.9547350.1951680.08162890.6357120.5978190.6381380.4968890.3374890.4538220.5871680.5904960.7359920.290690.2955670.1130530.3268470.1394280.5520150.7223410.6071010.898640.6370140.0361168
30.730910.509660.3032220.0402610.8525540.8470730.8625980.9078160.3834870.4214390.4532930.3391440.5498010.4642290.3096360.9408890.4863540.110190.8450750.08008780.06785710.2773220.2645330.9122350.7898230.5003690.5240780.9800490.0788040.6543150.375390.6021340.1517950.5280620.538450.8128620.4035070.5864740.8354720.1788770.6904860.6354530.8750240.4448620.5277150.4342910.8396370.2820830.2639740.3968560.634460.5521810.2316920.9146040.2422490.5120380.4305490.3649930.2844550.5204290.7748370.1375690.9246620.7022860.1871080.9865570.9855370.969260.8768670.7760120.1050040.6091120.07138980.3509110.3177260.8471530.1299480.7392090.3013270.5142560.398580.3340950.4022080.600680.6615280.7988780.1720260.4392990.344180.3076320.4897730.4198080.1389230.2310570.3499540.2219750.1302560.3155490.5888450.922503
40.1203170.4984910.4909960.360960.1837460.7106290.4206070.2658150.579460.4640010.237040.2559150.2000060.2559250.8401620.1426390.9123140.4489250.9205960.378770.2254990.9526090.2332260.5535960.4693940.3087770.5280250.7078680.7822730.3840460.7848840.7726690.6656270.1600230.4532330.8733240.07315930.4486550.7981690.9038030.06584720.4077150.3599080.2496240.1744590.960480.05152620.9228350.6813450.6336070.2738020.8120370.4604160.9126180.4558360.3113790.8918270.02887720.8972720.123370.3125890.4511330.6618450.1106790.7266290.0406770.1308470.7708060.3666210.1448140.9711980.5469940.5469170.8076440.7888830.1047180.1898190.4825780.8776470.4914760.8816660.8128110.3383890.6350050.6657020.5263560.1681470.2780040.5516160.2394680.2375770.4050980.4845010.02820260.02541770.1812160.7043840.2814180.7201580.725407
50.5684330.7619570.1542730.1964810.1593050.2612350.1115840.4530430.3602220.5174910.3669750.8458220.07488380.5256270.8694920.4144660.7825330.4875120.8389370.7067930.1410540.5555250.5199150.1945780.8521770.6867060.2679680.7391340.9409160.9755020.8708970.4459480.1757160.9574280.1287660.5098180.6347940.6355650.291610.8023260.3148440.7362660.7641960.446930.7390790.1800190.8911820.9554130.6440620.2429960.02108060.1449150.9378260.1778420.08789880.7732290.6174270.08764590.2750190.8632280.9446260.2212220.799450.7771320.003935930.5871180.3062820.3924560.6144360.6839450.3181710.583210.9565620.8895350.009507870.7447540.4846610.8474570.5062810.6652950.1317320.7060220.03818850.8135790.5587470.6654170.9909650.9915740.01786370.1031630.01717070.6342730.9490640.4300060.5247820.1675230.4019810.390690.6175410.504487
60.1458070.2907060.5323010.7093490.2326740.8853370.3065560.2597250.6170720.945690.8916360.5086180.3618330.1015560.9667760.03171070.5957030.2285510.457160.7494160.2968390.009573820.8784120.8728030.8351940.7195510.3345660.2494150.332530.9743270.08527960.5482020.7084620.7624170.1632910.3615040.3390850.8442350.7169780.07475250.4278670.9568940.5554110.5705950.9473660.5667580.8996150.7573990.7879410.8712860.3150830.5482640.5124660.1934920.2982230.05664360.2431750.3739210.8821720.2930410.7495920.3548830.07593630.8538490.3357770.7077450.02270870.6108590.6962110.7700220.3533880.9334740.2715170.3196380.3442630.9897120.6726580.7341140.9973180.941050.7118030.4944120.9387770.4300230.5278670.391110.9759970.3604780.7054490.2970180.03730860.9908680.06668280.8926160.1072850.9074680.7659290.7368660.2768860.470498
70.1260680.7936450.1046170.3939250.1646880.1178130.2203930.01671910.6854260.05559380.8111710.4143380.1047110.9563860.4756020.1698760.4223550.1263320.5209720.5074620.2232350.7411670.06668090.522260.5639150.3015910.1478830.7032160.7426210.2783220.2165510.8319780.6917510.8348350.4471340.1764810.3780290.4558490.4447070.5054990.7080340.4789880.5994650.477860.7450230.3048380.06961540.9035650.6249890.2041180.4832650.7292610.5983550.6121820.1130690.8045290.02862580.9713820.3844630.2489170.76610.4029130.6377750.188760.1188750.8784090.5301850.179920.5954220.9168830.9724480.8459910.6241130.04494850.4322520.7176630.413680.3596930.2022090.935750.2102690.5553920.4749290.2507780.1030260.8180850.05340830.04300140.5137330.8297760.7023150.4458990.2035210.3403970.4452610.4879740.1891650.953070.6540870.570371
80.0001394330.8034340.8240360.823670.4671220.4088280.6875270.1709330.1042220.3717310.6984810.3403720.5597460.1490280.911130.263060.6471630.1555750.4627940.3412310.07386460.6166860.3398440.7293920.5811830.8958560.3554780.7622420.06311530.08192680.1910520.6855040.08186770.491360.4446790.002775550.1627810.6256920.5098160.4577410.3311790.0223290.649020.3602540.1002570.006273340.5223230.5353650.955390.881770.2435280.8799630.8913050.5698190.7346870.5119490.6806280.3492780.2945120.95130.3428750.4365820.276950.9575960.3565090.04999830.214970.2243840.1594660.5886260.07523320.7431120.4143310.8947020.02760420.1429820.3803860.6658590.5287420.1054080.4044610.8136260.2924860.457950.3332910.004277170.3228610.4344050.07163590.512840.4764680.1685470.4341510.2234680.6860980.5499150.2608830.7213420.6225430.867771
90.4417610.9809870.02517490.4866990.4630750.296450.5574290.5000080.5291920.7824240.7079290.7270180.7249440.5587750.7432610.9840760.5164930.7022710.2125750.9471660.9967890.3539830.4610620.08959260.3689080.9995410.9921960.626520.8145970.992910.8861250.5819220.956270.3911580.7980850.2360940.8746050.2215460.8722610.2406060.09300770.1358740.1180110.1120940.7908450.163530.5138630.5235050.3625780.1499650.4906930.3614190.7677350.8918420.7063390.5107270.1098570.9539390.6570460.8393680.1540810.1108150.07974040.3519160.2306040.9158470.2554690.2739850.5091990.9382320.5641570.007815920.6219460.613970.1420750.3408740.9941380.9407960.4224820.03084320.8066960.4228620.4417610.9770130.3517660.5368030.3723690.4994050.5403940.4349340.9382160.03212340.5431450.1798770.7590050.5379290.8404060.03022620.2543250.0349914
100.9327170.1362910.1783040.6806850.05404470.7495470.7260930.06276820.288650.6748920.7347420.9513530.3371940.8501440.8586010.9803980.6262140.7319270.03122990.5330610.3893790.2409980.8543360.1529670.9744990.7195780.2036730.2548430.009514270.3858830.8989670.0490080.5748030.043810.4268680.7463770.5773890.7924060.7900790.3356260.2463940.1124240.4838440.3803370.2467090.279820.2533150.317280.8538590.2011680.2723520.6823710.7875560.9828090.684510.318330.1465780.03714160.5780850.1705680.9719670.593350.01755960.2417260.372420.7294690.3898340.6032770.8432340.7468980.0347050.603160.5129670.3542610.1738470.4052740.32590.7891320.5342970.9297360.4957860.1317530.4499480.08050490.1309790.446670.5851650.9151570.8452970.3417710.8159010.3208190.9678730.02961240.7788940.337420.3308810.09340380.8105970.587841
110.1274910.2576910.1290450.4757750.3262730.964290.5185440.4504570.5111190.7613590.9986270.285150.7729940.5805210.19850.8720510.05969110.9390580.08452530.1803750.8707010.1754150.2406410.1392370.9192370.7066110.6532290.9440460.6123790.08038720.09133230.6510080.168060.7745990.5934080.8286750.079710.6772120.8938440.7472010.2298920.616840.9266920.5198240.8027150.3846490.2643980.5849150.2281970.5847930.1402770.4665940.1936190.9721520.5077480.2707740.2293460.6763110.922720.991360.05013390.1031390.09309910.6978210.03472650.6876280.6476940.1586480.9713750.6814280.2135520.5387610.5908950.3626930.1359580.5730920.415720.6902760.9273810.870420.614670.5847770.05582980.2879650.427460.3916880.6051470.9782970.8909550.005181540.03565610.002644540.1087460.5577850.1306980.07642030.3773980.398390.01975450.104785
120.4099770.5466770.2668150.6883510.7435870.1318510.7918820.9519060.00845110.0643160.8626750.9700820.9992580.486410.06476930.3032870.8411170.6782910.8849130.2557220.6435660.5561810.8335950.3061060.6119660.4225530.9413440.2695430.2796780.8072840.04451860.479560.9013540.7802190.6579370.2573660.3762410.2998870.3414050.005972760.8048320.6102580.9800990.9292810.5858570.1963540.5009880.7029840.772970.2871540.9770410.6021610.05188380.6986060.6418010.2583970.9072140.3448560.374940.3380270.6241930.5466720.5648980.03552180.3466210.4439830.09450570.006395060.4421490.545680.2045090.2826130.4583420.5661160.3063050.5792610.8560550.9076710.8746950.5398750.9724810.786060.4314960.1738560.4575250.5650110.8791460.1072610.2725330.969410.9453850.4384470.4698770.4515310.5432380.8861960.4541550.6630350.07216540.0636156
130.5755160.40140.3876980.633280.6505550.784710.5059190.7766160.04717740.8325590.2705230.09948570.7912340.5699250.09153650.7958580.2049890.4750560.4323130.6102040.2954070.325230.9998660.3419680.985480.08969240.8996890.6159550.8682420.6564720.03259840.9771260.1022160.9626210.9991830.2369840.9575040.6686120.5049620.9225290.0906130.5004230.6228110.6491090.4578220.7397920.2005560.08718440.4338410.3660410.2555590.2953670.1111760.03172940.4393690.3041830.03519450.673310.2900190.4113740.7487140.8517210.5870070.3686660.971470.3261580.456960.9472480.9843180.5800970.7510010.8876510.04543690.9973060.7926860.177230.5132610.7834850.7376250.622530.4509950.07751530.5479330.2310960.04201630.4712580.9440360.06236420.1906690.2723180.5199310.2839910.230590.4784130.8714860.327440.569750.5426460.766730.596762
890.4468750.2471070.2848260.5566440.07272570.2605610.2069810.1677580.1356830.3682550.8034260.04772210.6917460.2247770.7065430.9590740.7411020.7596540.9660210.01337730.6471330.1561730.3701390.03740460.9649070.2404740.697660.007942130.3235590.5914610.9722460.3087490.6930560.4330990.1191470.7170880.8296990.2610650.9089230.1216630.2347870.3706040.8305460.1356780.5788570.09914090.5057380.1146450.2180030.269680.1456330.3104620.3022750.9212760.876580.7338020.6811790.1328510.4739240.9714990.06941620.5952050.3576920.4558730.7864390.5636560.1400370.7334650.2447050.7664670.5876610.7530230.6276270.8565320.7193450.8888470.3349970.07670250.8607960.2831970.1194110.1835140.4937820.1593520.2928040.556550.4873640.3266650.06266440.4852440.2762020.6537640.5417870.1237590.6077770.1530380.6226010.1939580.1023140.984599
900.1473070.4903310.639080.5926660.4777060.4264090.7103940.3868210.9289930.22280.8264480.6833190.9543570.7699880.5055120.4956720.1305530.187660.1795210.01599610.4008390.1919470.6319710.8932160.6004210.7966230.3893350.587770.563450.09010360.7609560.5029190.7772630.09007720.651870.2633670.7234380.817350.676340.65270.8411120.6623030.9253330.4927120.9086070.8193450.2047490.3719910.6129310.3888850.3258960.8231870.5200160.3948870.9533040.2859190.7149560.9153350.8717250.09695420.1869070.6996510.41240.3830660.1549260.5879690.3797250.08336320.6408310.6661670.8842440.2987610.9121510.5586270.03523570.6590430.2478360.130310.5597130.5800850.484780.9550040.8677570.4601060.3184280.8561080.208710.8874020.9600150.8893170.935870.2572710.5273630.1945760.2814660.941540.6335480.6585810.4297450.174617
910.4047620.02572870.3693960.2569910.07557910.01525160.3165140.4543790.8500580.2523180.8661970.5034390.2829640.7234530.7597830.5959580.5075840.5143990.1564250.7828680.4540550.1347740.9586720.07718380.3018560.5624840.3637540.5871440.04529050.2175870.8591220.8745280.09455780.6095130.9020720.9548280.7914930.4525420.2454360.2957950.7714810.9133230.1245040.6062750.61530.7908810.1297510.723620.3526340.7879690.7118740.07460450.002623660.2487760.0029030.5921380.7339490.4911280.8943540.6667030.7606070.1112090.07604660.3139430.9111380.7505480.5731670.6415880.2181780.6305510.1963810.6526280.2538790.2205450.7201120.6225710.4659490.9172360.7025620.6025230.2117770.7814540.527780.4731580.221420.1131360.9713660.09820240.444040.2169820.2987590.1164850.6291340.05863610.1174950.3316220.8358840.1202380.7948440.281144
920.1196120.04701450.5685960.5716630.5561010.03131650.3722350.4963410.5906470.4287490.6607940.4296350.6209870.4785470.6616780.02092940.4042230.6743540.894710.7147650.5946550.0172920.9747340.1902180.3545390.1793730.9454260.6848770.0836780.5070030.9329640.9893860.9464730.6263140.6943310.7463320.03778940.8785870.574450.3361940.2778170.2922590.1897080.9407360.3552640.5130180.6882740.4083790.7205820.04293910.6449330.5202980.858920.004716750.06116690.7519460.1820490.9660.007270190.03444920.3282690.2017010.2616970.08944320.3323240.564140.2954110.7958940.4719850.4312580.5448190.4160340.6689570.7000880.453290.3048410.3349440.6351770.6548450.6185070.428850.09760640.8261970.3436730.7945680.1678740.2558990.969850.2384780.1539540.7455940.6544960.05431420.7972650.4182850.4735540.9985860.1697090.5759530.0273939
930.5031260.5577920.4699960.5673360.4939450.4687630.618340.7754350.5885610.0384720.3426380.7902540.6891630.9657760.7897330.5115310.4242040.7791890.2679650.3265960.9024930.1353080.9354410.5154560.9342460.6588610.3522360.39370.7009410.4338920.2010440.4479770.1569760.973430.7404580.4920060.8084510.6203250.7328740.4916790.2565720.8025180.8701730.4917440.152890.6777860.8016150.8192650.725280.6146170.9078080.3922430.2140480.04770930.2681920.7988190.6537850.5180310.8395220.6010480.2233790.05502930.1991380.3817180.3470190.7908620.1167680.9241570.3357950.3021250.7095670.695680.1159120.7956860.7086020.157070.6148650.279250.3861510.05175880.2735390.6150750.1913070.1328710.6105990.884340.583960.4861450.9357080.3713160.5164980.8709310.9983280.4177490.5715090.4764580.02210010.7934450.4009410.273983
940.1921350.1540830.8245240.08065650.8047410.9638020.7097280.5296960.487940.4030260.5476370.9621710.6977780.4487450.9303140.2154280.03844380.6163750.8791180.3250390.2940320.8891730.7386130.1030950.2630390.8293120.9162250.1222920.7845260.5897860.3431340.8932530.01917580.6112150.6313150.007428560.3207290.04132670.4271360.6099410.9328770.02360960.8333970.1031320.7647750.6138240.653990.9998620.4132490.9365210.3552790.9383540.1967030.06759080.4175690.9948490.5755070.4372440.5683750.9470190.807790.5117140.04280970.2086460.01818240.6203780.9111230.6208150.0146360.604040.7730580.7001830.3533040.1293270.5067470.7189180.8071180.2832940.5838250.6543920.591550.89390.2355360.1966520.4772260.7212920.306760.3585510.2860170.9087060.7154940.1404640.1337340.7799240.2185680.9777510.6518250.4580980.5421150.0197272
950.526780.9515850.6283440.2423650.05711840.8417270.05912490.5372630.1128910.2959480.5391840.4397520.6519770.01832360.7168350.9672120.660470.411110.5893980.2138830.1869980.3391070.7852310.9803160.1900280.7140570.6267510.101670.7964810.9899380.9961170.6433120.2269040.5723970.3348330.5831090.8637220.7353270.664480.06041290.02872550.9347060.02445020.1632580.1152250.8030180.7009870.5212910.2898990.2310580.2869740.3308150.9743410.4298940.6312540.285710.2737470.7616070.3760120.2263860.2886510.5927560.9248070.4405520.1593710.394240.853850.9301990.5295650.791440.6269580.579390.1663690.0660690.5637840.7178780.1227560.3279930.8856370.06268310.4132020.0868540.338910.7834470.4585040.05074190.5427770.3434440.737180.562190.542750.2742520.1709570.06162790.9821540.4114620.1573680.3712470.4532050.499968
960.6232410.1093840.5670260.6331580.8048040.1647940.5832390.5201720.5978270.7577970.08096620.0924680.5300160.9983110.4439460.1223910.2460280.1505690.893610.3067910.3845470.1082650.02136130.9152140.3749370.03523690.1356940.2496750.7697290.8157790.781130.5108740.5080410.6387280.2593520.5666180.7733130.1855960.1243230.3141830.9758980.430580.4916430.8360060.8393930.8991770.4178050.08492160.2756490.860320.6877950.7504050.8908420.1757660.1698720.5931890.2482610.7228670.08533380.8536840.7611060.2510160.3423290.6571530.5869970.516270.2744210.5832770.130890.9566580.3311340.9489440.005865760.6127750.7361720.9200540.2166140.673180.9123380.01636290.797960.6901010.4632250.995880.9624550.3363710.3499370.360730.3164280.774690.1347210.9205950.79930.7955380.8862490.8377640.3699770.6243030.7605910.173201
970.003596030.500720.8660550.6061290.03098370.7704990.03691870.9675340.344810.992590.2345190.7610410.9141610.9976530.2861730.9010740.951860.9183070.2876890.2031510.5671930.9496990.8548520.449660.2177860.8576230.3249950.262080.4007180.3195580.6879570.4439030.750070.06399020.4583580.9862360.9489910.009878130.1840460.9552670.687370.2415940.45560.1731840.4439310.2415960.886690.470040.5611870.1183160.6026450.1994450.6951170.347750.09126610.3928480.3179620.9978410.05850720.8119370.2701540.978240.1578550.4647520.7429660.5252430.6982510.2153970.3426070.3099980.5375950.9322030.5743460.09060140.4407810.3540950.03953840.7011060.4507320.9781070.7496190.2688850.06158040.1774210.1539990.04739210.3436840.8619220.8621120.900110.9990440.3854540.1247850.7048390.9662420.2793880.7590540.6164410.3597580.306592
980.134040.3098090.9631180.08548160.2259470.9883340.9301640.3344330.3255180.4930960.4762380.048930.0357440.3386120.08838390.4819260.6636570.7158870.2640.5748210.9398320.4780518.87875e-50.6917610.2015850.1185640.695490.8490690.06419920.557460.3580340.3710750.778960.5144690.8047320.7076590.9654650.7259880.2025720.9845350.1797290.7672450.7826570.2069540.06250470.490980.399630.1088970.04895550.6520710.9858950.7213770.8977970.9339580.1348610.5520090.2383990.290520.9698250.5167510.306280.2187070.6445950.4164160.1410920.7191810.7147990.7810560.4627660.04420680.2801020.5788580.9342840.2282070.009716660.6161850.5148040.9119980.4839820.2550150.8996320.4871660.166150.3400850.006778540.1776470.917910.6468820.3470270.1152460.4247930.4766090.5232170.9354820.6001680.8065240.8977430.9236510.1850420.00802243
990.5460050.9267870.7869190.02822360.6813650.9098860.9179470.3218970.9615780.2903180.9658720.5913120.6610290.03431630.3901450.2428130.06581920.002902590.5814030.1938870.7151620.5266610.2203450.8435030.08470040.5772630.7612460.8727640.9085820.6111240.6833220.7629490.3685980.9131580.2364020.626130.6602580.7898780.2719480.89120.06978760.7017470.6475580.3196150.141740.1581290.7973590.8560790.1667280.1678980.1787490.5440760.9833940.8486640.01441160.5117290.1278530.8634710.3436210.1451720.4101680.3059290.5298530.3512020.8976990.5605710.5954790.4788650.9297950.7472470.001995250.7659470.3483630.0763280.5538960.3338880.1503770.3261540.9478520.4052220.4354550.1910410.2052020.9244530.291470.686040.2618610.87580.1082260.05358750.6540440.1512790.9846880.8837610.9302090.9374290.9143690.5612890.2467260.433878
1000.4733950.9027510.3575670.5196760.8265540.5139270.1866780.3605560.3771010.8578030.7728570.1190860.006648570.5103920.8453130.2447230.2314810.03949840.2125710.2341720.652550.9611540.4121040.5365110.6231550.9084910.5149130.309240.0536970.08904970.8249580.6942830.5328990.5558360.5937460.8722930.7011790.4695780.1364910.527440.4218160.4066540.6660790.1330070.8930650.5767360.319050.9505790.7076180.5617180.8409210.008393850.2877840.8304810.8496430.1353220.7459690.1430080.7400880.6236560.9253870.9339810.6348340.5527130.218190.2132930.3029780.9269430.4403410.2785460.8210510.4155720.09941070.06478090.7687550.1200090.07451610.1998430.005251640.05560040.3190730.2431480.6319640.4375530.07003450.2648550.7950130.7443420.8027190.3656710.5337540.651170.149430.25120.3127360.2802170.8982140.1863970.7995310.115451

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.685539    0.280285  0.95708   0.547698   0.578914   0.217532  0.383553   0.741661  0.991897  0.0632198  0.687152  0.349731  0.469358    0.147015   0.548728   0.0129153  0.786575   0.581007  ⋯
  ⋮  │     ⋮          ⋮         ⋮          ⋮          ⋮         ⋮          ⋮         ⋮         ⋮          ⋮         ⋮         ⋮          ⋮           ⋮          ⋮          ⋮          ⋮          ⋮     ⋱
                                                                                                                                                                          83 columns and 99 rows omitted

Most elementary get and set operations#

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

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

all get the vector stored in our DataFrame without copying it

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

the same using string indexing

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

note that this creates a copy

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

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

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

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

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

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

x[1, 1]
1

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

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

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

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

! indicates that underlying columns are not copied

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

: means that the columns will get copied

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

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

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

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

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

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

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

Caution

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

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

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

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

x = DataFrame(rand(4, 5), :auto)
4×5 DataFrame
Rowx1x2x3x4x5
Float64Float64Float64Float64Float64
10.4546010.9179260.2065110.6017650.14814
20.1252390.6855640.7160210.4470670.74465
30.6005380.4583510.9671670.3700340.788899
40.07780190.679830.9899730.1394590.0781966
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.9179260.2065110.601765
20.6855640.7160210.447067
30.4583510.9671670.370034
40.679830.9899730.139459
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.4546010.9179260.2065110.601765
20.1252390.6855640.7160210.447067
30.6005380.4583510.9671670.370034
40.07780190.679830.9899730.139459

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

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

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

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.4546010.917926
20.1252390.685564

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.3194420.9557230.329135
20.9593390.4918230.861783
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.3194420.9557230.329135
20.9593390.4918230.861783

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.3194420.9557230.329135
20.9593390.4918230.861783
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.