2

I have a dataset which has a column called "PS_I". I cannot change this because I imported the column from an existing csv.

I would like to add another column. Such as:

data = data[All, Append[#,"newPSI" -> #PS_I * 3]&]

But that doesn't work due to underscore being a predefined character.

How do I get around this?

EDIT: Here's an example.

This is one column with such an index:

data = Dataset[ {<|"PS_I" -> 1|>,
   <|"PS_I" -> 2|>,
   <|"PS_I" -> 3|>,
   <|"PS_I" -> 4|>,
   <|"PS_I" -> 5|>,
   <|"PS_I" -> 6|>,
   <|"PS_I" -> 7|>}]

And I want to create a new column with some operation on the values of the PS_I column. Kind of what this answer suggested: How can I add a column into a existing Dataset?

The underscore does not allow for these type of operations, and I would like to know how to deal with this problem.

EDIT2:

Here's how to reproduce the problem, now with input from JasonB:

1) Create a csv with these contents: http://pastebin.com/eYUaW2bV

2) Run

data = SemanticImport["<pathtofolder>/example.csv"];

3) Try to append with

data = data[All, Append[#,"newPSI" -> #"PS_I" * 3]&]
triplebig
  • 287
  • 1
  • 8
  • 1
    What about using quotes about the key name? – Sjoerd C. de Vries Nov 21 '16 at 20:10
  • 2
  • Hi JasonB, I found that topic but it did not help. Placing quotes around it gives me an error: "Key PS_I is not one of {..., PS_I,..}" – triplebig Nov 21 '16 at 20:21
  • 3
    I enter data[All, Append[#, "newPSI" -> #"PS_I"*3] &] with your definition of data and it works: http://i.stack.imgur.com/3Yhs2.png – Jason B. Nov 21 '16 at 20:22
  • That's strange. This means that when importing it from a CSV, the underscore may be parsed differently? Could you please use do:

    data = SemanticImport["pathToCSV.csv"];

    With a csv with the same values?

    – triplebig Nov 21 '16 at 20:24
  • 1
    @triplebig - can you paste the CSV contents into pastebin.com and give the URL here? – Jason B. Nov 21 '16 at 20:27
  • in fact, i think that the official stance is that you're supposed to use quotes in key names like that, elsewise you might run into all sorts of odd issues. (this would be far from the weirdest one, really) – Ben Kalziqi Nov 21 '16 at 20:34
  • @JasonB There, edited the topic with your request – triplebig Nov 21 '16 at 20:35
  • @triplebig - when I use SemanticImport on your data file, whether downloading first or just grabbing it from pastebin, it labels the first column as "PS_V PS_I" and the second column as "column2". See here. That's why you can't access a column called #"PS_I" – Jason B. Nov 21 '16 at 20:41
  • SemanticImport is getting confused by the leading tab on the header line and is treating the whole line as the name of the first column. If you delete that leading tab, then all is well. Incidentally, the file is TSV not CSV. – WReach Nov 21 '16 at 20:42
  • Arg, sorry. That's still not the original problem, as the original data I have (which I can't paste) does not show this problem. One sec while I try to get a better example. – triplebig Nov 21 '16 at 20:45
  • With the errors in your pastebin file corrected, the results are as desired – Sjoerd C. de Vries Nov 21 '16 at 20:51
  • After I cleared all my global variables, it worked as expected. This was probably an issue with pre-defined data. It was confusing in that the error message I saw was exactly "Key PS_I is not one of {..., PS_I,..}", so I did not suspect that this would be the issue. – triplebig Nov 21 '16 at 20:55
  • I'm voting to close this question as it stands, but if there are still lingering issues that need addressing please update the question accordingly. – WReach Nov 21 '16 at 21:12
  • Hold up, I'm trying to reproduce the original problem I had (I'm not so confident it was a "global variable not cleared" issue), but I agree to close it in a few more days if I don't update. – triplebig Nov 21 '16 at 22:58

0 Answers0