Skip to content
Advertisement

Pandas DataFrame and grouping Pandas Series data into individual columns by value

I am hoping someone can help me optimize the following Python/Pandas code. My code works, but I know there must be a cleaner and faster way to perform the operation under consideration.

I am looking for an optimized strategy because my use case will involve 16 unique ADC Types, as opposed to 4 in the example below. Also, my initial Pandas Series (i.e. ADC Type column), will be several 100,000 data points in length, rather than 8 in the example below.

JavaScript

The initial DataFrame (i.e. df) is:

JavaScript

I then manipulate the DataFrame above using the following code:

JavaScript

The returned DataFrame (i.e. df_concat) is displayed below. The ordering of RAW and the associated ADC Type values must remain unchanged. I need the return DataFrame to look just like the DataFrame below.

JavaScript

Advertisement

Answer

I liked the idea of using get_dummies, so I modified it a bit:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement