0
$\begingroup$

Actually I load .csv file about movies to Mathematica and store it in "dt", and there is one column records the production countries which is the first column df[[All,{1}]]. And the format of one row is like {[{'iso_3166_1': 'US', 'name': 'United States of America'}]}, but some of rows have different length like {[{'iso_3166_1': 'FR', 'name': 'France'}, {'iso_3166_1': 'DE','name': 'Germany'}, {'iso_3166_1': 'IT', 'name': 'Italy'}, {'iso_3166_1': 'US', 'name': 'United States of America'}]}, so I wanna extract the production countries name regardless how many countries in one row. Therefore, for the first one is US, and second one is FR, DE, IT, US. Then I want to count that US shows twice, the rest shows once.

The first pic is csv file, the second one is read in Mathematica

.csv file [dt[All,{1}]

$\endgroup$
5
  • $\begingroup$ Include the code you have used so far, and at least a sample of your original data as text rather than an image. $\endgroup$
    – MarcoB
    Commented Apr 10, 2022 at 11:04
  • $\begingroup$ github.com/yeqinL/Mathematica-Project/blob/main/… This is the link for the .csv file $\endgroup$
    – Yeqin Li
    Commented Apr 10, 2022 at 12:07
  • 2
    $\begingroup$ This doesn't look like a CSV file, Why all the entries are inside square brackets? Inside some of the square brackets, there is more than one element? Conventionally a CSV file is a file with comma-separated values. $\endgroup$
    – Galilean
    Commented Apr 10, 2022 at 13:57
  • 1
    $\begingroup$ "It's about"??? Can you write a clearer title please? $\endgroup$ Commented Apr 10, 2022 at 17:42
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Commented Apr 10, 2022 at 22:21

1 Answer 1

1
$\begingroup$

The main file is not a CSV file but with a little of replacing it could be:

  1. Import the file as a plain file
raw = Import["C:\\movies_metadata.csv", "Text"];
  1. Remove unnecessary characters and replace : to , and import the output as a CSV string
data = ImportString[
   StringReplace[raw, {Characters["\"[{}]'"] -> "", ":" -> ","}], 
   "CSV"];

Now you could select the countries of the first 10 rows:

data[[2 ;; 10, 4 ;; ;; 4]]

(* Out: {{"United States of America"}, ... , {"Germany", "United States of America"}, ... } *)

Replace 10 with any number you want. The main point is in selecting the columns which have step size 4 (2 is for skipping column names).

$\endgroup$
1
  • $\begingroup$ Thanks for your help, it definitely helps a lot!! You give me a new way to solve the problems. $\endgroup$
    – Yeqin Li
    Commented Apr 11, 2022 at 16:05

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.