7
$\begingroup$

data

Hi, can anyone help me with this? I want to delete the column "FIPS" and "Admin2", and keep the dataset as the original. How would I do this?

data = Import[
  "C:\\Users\\49176\\OneDrive\\Desktop\\time_series_covid19_confirmed_\
US(2).csv", "Dataset", HeaderLines -> 1]

Code is here, and the csv file is downloaded from https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data/csse_covid_19_time_series

$\endgroup$
4
  • $\begingroup$ “ and keep the dataset as the original” - so you do not want to modify the original dataset, you just want to create a new one with those columns dropped? $\endgroup$ – MarcoB Dec 3 '20 at 3:40
  • $\begingroup$ My bad, I want to modify the dataset, not keep it as original $\endgroup$ – jfm Dec 3 '20 at 3:41
  • $\begingroup$ Please provide the Mathematica code for your Dataset. (Also, please keep the image because of the answer by @kglr .) $\endgroup$ – Anton Antonov Dec 3 '20 at 13:31
  • $\begingroup$ Thank you for reminding me of that. I have provided the code and the resource of the csv file. $\endgroup$ – jfm Dec 4 '20 at 3:54
14
$\begingroup$

You can use KeyDrop:

picture = Import["https://i.stack.imgur.com/p7ax0.jpg"];

table = Transpose[Partition[TextRecognize[picture, "Word"], 9] /. "Long_" -> "Long"];

ds = Dataset[AssociationThread[First @ table, #] & /@ Rest[table]]

enter image description here

KeyDrop[{"FIPS", "Admin2"}] @ ds

enter image description here

We also get the same result using any of the following:

ds[KeyDrop[{"FIPS", "Admin2"}]]

Query[KeyDrop[{"FIPS", "Admin2"}]] @ ds

ds[All, Delete[{{"FIPS"}, {"Admin2"}}]]

ds[All, Delete[{{1}, {2}}]]
$\endgroup$
5
  • $\begingroup$ Thank you! It is really helpful! $\endgroup$ – jfm Dec 3 '20 at 4:50
  • 1
    $\begingroup$ @jfm, my pleasure. Thank you for the accept. Welcome to mma.se. $\endgroup$ – kglr Dec 3 '20 at 4:51
  • 1
    $\begingroup$ Nice use of TextRecognize! $\endgroup$ – Anton Antonov Dec 3 '20 at 13:30
  • $\begingroup$ This looks like it generates a new dataset with the column dropped, is that right? Is there any way to drop the column 'in place', i.e. without copying over the entire dataset? That is, the only way to have the column deleted here is for one to do 'ds=ds[KeyDrop[{"FIPS", "Admin2"}]]'. (But I can see that this isn't the question in the OP.) $\endgroup$ – berniethejet Feb 20 at 16:38
  • 1
    $\begingroup$ @berniethejet, see this answer by WRech re modification in place and Dataset. $\endgroup$ – kglr Feb 21 at 0:26
2
$\begingroup$
list = {
   <|"a" -> 1, "b" -> "b1", "c" -> {1}, "d" -> 1, "e" -> {1}, 
    "f" -> 1|>,
   <|"a" -> 2, "b" -> "b2", "c" -> {2}, "d" -> 2, "e" -> {2}, 
    "f" -> 2|>,
   <|"a" -> 3, "b" -> "b3", "c" -> {3}, "d" -> 3, "e" -> {3}, 
    "f" -> 3|>,
   <|"a" -> 4, "b" -> "b4", "c" -> {4}, "d" -> 4, "e" -> {4}, 
    "f" -> 4|>,
   <|"a" -> 5, "b" -> "b5", "c" -> {5}, "d" -> 5, "e" -> {5}, 
    "f" -> 5|>,
   <|"a" -> 6, "b" -> "b6", "c" -> {6}, "d" -> 6, "e" -> {6}, 
    "f" -> 6|>};
list // Dataset
list[[All, 3 ;; 6]] // Dataset

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.