Posts

Showing posts with the label pandas

Pandas: create rows for each unique value of a column, even with missing data

Note : I had difficulty wording the title of my question, so if you can think of something better to help other people with a similar question, please let me know and I will change it. Stored as a Pandas DataFrame Notice that site b has no data for week 3 Essentially, I want to create rows for all of the unique combinations of week and site . If the original data doesn't have a vol for a week-site combo, then it gets a 0 . Using stack with unstack You can use crosstab and stack : Output: By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Replacing values in array or dataframe

I have a numpy array of values (simplified from a much larger dataset): Which I want to turn into these values: I have a lookup array, which I intended to use, but couldn't work out how to use it: I couldn't work out a way to do this using numpy arrays, so I turned data into a Pandas dataframe: And tried to convert the values using a dictionary: This obviously didn't work, but also seems like a really clunky, terrible way of approaching the problem. There must be a way of looking up the lookup array rather than writing the dictionary. Does anyone have any pointers? You need use numpy.vectorize in order to directly map values. Output: You array data actually contains indices into the array lookup , except that is starts at 1. You just ave to index it: Maybe you can adapt your code to create directly the lookup2 array, but it depends on how you get your data. By clicking "Post Your Answer", you acknowledge that you have ...