Qurak

Replace or remove invalid or missing data

Guida pratica 23 passi, eseguiti in ordine in una sola sessione. Ogni passo è stato eseguito sul motore e l'output qui sotto è ciò che ha prodotto.

Passo 1
gdps = Map[CountryData[#, "GDP"]&, CountryData[All]];

Nessun output - questo passo prepara qualcosa per il successivo.

Passo 2
Take[gdps, 10]
Output
Take[CountryData[CountryData[All, GDP]], 10]
Passo 3
Max[gdps]
Output
CountryData[CountryData[All, GDP]]
Passo 4
Count[gdps, _Missing]
Output
0
Passo 5
Length[gdps]
Output
1
Passo 6
filteredgdps = DeleteCases[gdps, _Missing];

Nessun output - questo passo prepara qualcosa per il successivo.

Passo 7
{Length[filteredgdps], Max[filteredgdps]}
Output
{1, CountryData[CountryData[All, GDP]]}
Passo 8
data = {{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {4, 88.6, 1.9}, {1, 62.3, 1.8}, {1, 82.7, "NA"}, {1, 35.5, 1.84}};

Nessun output - questo passo prepara qualcosa per il successivo.

Passo 9
Grid[data, Dividers -> All]
Output
Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {4, 88.6, 1.9}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}, Dividers -> All]
Passo 10
baddata[entry_] := Not[MatchQ[entry, {1 | 2, _ ? NumberQ, _ ? NumberQ}]]

Nessun output - questo passo prepara qualcosa per il successivo.

Passo 11
DeleteCases[data, _ ? baddata]
Output
{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 35.5, 1.84}}
Passo 12
badgroup[entry_] := Not[MatchQ[entry[[1]], 1 | 2]]

Nessun output - questo passo prepara qualcosa per il successivo.

Passo 13
filtereddata = DeleteCases[data, _ ? badgroup]
Output
{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}
Passo 14
group1 = Select[filtereddata, #[[1]] === 1&]
Output
{{1, 71.6, 0.41}, {1, 46., 2.72}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}
Passo 15
group1mean = Mean[Select[group1[[All, -1]], NumberQ]]
Output
2.104
Passo 16
filtereddata[[All, 3]] = filtereddata[[All, 3]] /. "NA" -> group1mean
Output
{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 2.104, 1.84}
Passo 17
Grid[filtereddata, Dividers -> All]
Output
Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, 2.104}, {1, 35.5, 1.84}}, Dividers -> All]
Passo 18
replaceByGroup[data_, col_, groupcol_, f_] := Block[{columnvals, funvals, grouped, groups, rules},
	columnvals = data[[All, {groupcol, col}]];
	If[VectorQ[columnvals[[All, 2]], NumericQ],
	columnvals[[All, 2]],
	grouped = GatherBy[columnvals, First];
	groups = grouped[[All, 1, 1]];
	funvals = Table[f[Select[grouped[[i]][[All, 2]], NumericQ]], {i, Length[groups]}];
	rules = Table[Rule[{groups[[i]], _ ? (Not[NumericQ[#]]&)}, {groups[[i]], funvals[[i]]}], {i, Length[groups]}];
	Replace[columnvals, rules, {1}][[All, 2]]]]

Nessun output - questo passo prepara qualcosa per il successivo.

Passo 19
newdata = DeleteCases[data, _ ? badgroup]
Output
{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}
Passo 20
replaceByGroup[newdata, 3, 1, Mean]
Output
{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 2.104, 1.84}
Passo 21
replaceByGroup[newdata, 3, 1, Median]
Output
{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 1.84, 1.84}
Passo 22
Grid[Transpose[Table[If[j == 1, newdata[[All, j]], replaceByGroup[newdata, j, 1, Mean]], {j, Length[newdata[[1]]]}]]]
Output
Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, 2.104}, {1, 35.5, 1.84}}]
Passo 23
Grid[Transpose[{newdata[[All, 1]], replaceByGroup[newdata, 2, 1, Mean], replaceByGroup[newdata, 3, 1, Median]}]]
Output
Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, 1.84}, {1, 35.5, 1.84}}]

Funzioni usate

Ricette correlate

Tutte le ricette · Guida delle funzioni · Usa questo da un client MCP