0
$\begingroup$

This must be very simple but I could not find an appropriate answer. I have imported a huge .xlsx file with three columns: country name, date and a value and I would like to DateListPlot the values against the dates only for some countries. This is the code used:

DateListPlot[data[1 ;; 720, {"date", "value"}], PlotRange -> All, FrameLabel -> {{None, None}, {None, HoldForm[Austria]}}]

Instead of adding manually the row numbers (1;;720), I'd like to tell Mathematica to plot the date-values for rows having "Austria" on column 1.

Thanks!

$\endgroup$
4
  • 1
    $\begingroup$ Welcome to the Mathematica Stack Exchange. Can you please show us the Mathematica code for how you have imported the file? Can you provide a 1-click link for downloading your file? Without looking at the format of the actual data, it is rather difficult to discuss the potential solution(s) further. Please also indicate using a tag if this is a homework question. $\endgroup$
    – Syed
    Commented Mar 2, 2022 at 15:05
  • $\begingroup$ Hi, thanks for your prompt answer. The import code: SemanticImport["/home/bs/Desktop/data.xlsx"] $\endgroup$
    – Beny Spira
    Commented Mar 2, 2022 at 15:21
  • 1
    $\begingroup$ You can Select on the country name. $\endgroup$
    – Alan
    Commented Mar 2, 2022 at 15:48
  • 2
    $\begingroup$ Read this workflow to learn about filtering a Dataset. $\endgroup$
    – Ben Izd
    Commented Mar 2, 2022 at 15:50

2 Answers 2

1
$\begingroup$

Since you mentioned that you used SemanticImport, I'm going to assume that the result in Mathematica is a Dataset. I created some dummy data and imported it. Check your actual data against this result (you can use FullForm to see all details). If your data is significantly different, then some of the subsequent computations may not work.

data = SemanticImport[pathToFile]

enter image description here

data // FullForm
(*should see DateObjects and Entity["Country",...] expressions *)

You can perform sophisticated queries on Datasets, but the following is just a simple "filter". If your data doesn't actually have Entities in it, then this needs to be adjusted.

austriaData = data[Select[#Country == Entity["Country", "Austria"] &]]
(*should see a smaller Dataset restricted to just Austria*)

DateListPlot can use data in the Dataset form, but it expects just 2-value rows (date-value pairs). So, we can constrict our rows to just the plot-able data like this:

austriaData[[All, {"Date", "Value"}]]

Let's just feed that to DateListPlot:

DateListPlot[austriaData[[All, {"Date", "Value"}]]]
(*should see a plot*)

From there you can hopefully move on to adding the plot decorations that you want.

$\endgroup$
1
  • $\begingroup$ It worked! Thank you. $\endgroup$
    – Beny Spira
    Commented Mar 2, 2022 at 19:08
0
$\begingroup$

We first need some data to work with. I create some random data with 3 columns: Country, Date, Number

dat = Table[{RandomChoice[{"Austria", "Gemany"}], Date[], 
    RandomInteger[{0, 100}]}, {20}]

enter image description here

Now we may use "Select" to pull out all rows with "Austria" as the first element:

Select[dat, #[[1]] == "Austria" &]

enter image description here

$\endgroup$
1
  • $\begingroup$ Couldn't make it. Perhaps because data was imported with SemanticImport? $\endgroup$
    – Beny Spira
    Commented Mar 2, 2022 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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