Split mixed type DataFrame into two columns?
Split mixed type DataFrame into two columns?
I'm munging a report I loaded into a DataFrame. The report's SKU column has mixed datatypes. I want to split the column into two new columns (SUBTOTAL and SKU) based on cell data type (str, int).
Following the example from a similar question I get a boolean column. Ok
df['SUBTOTAL'] = df['SKU'].apply(lambda x: isinstance(x, str))
SKU AMOUNT SUBTOTAL
7 4410 1 False
8 4200 5 False
9 total 6 True
11 4250 0 False
12 4255 0 False
I'm doing this in a Jupyter Notebook. Here's the thing that's driving me crazy. If I first call the above line, and wrap the code with df
, and rerun that cell, I get what I want.
df
df['SUBTOTAL'] = df[df['SKU'].apply(lambda x: isinstance(x, str))]
SKU AMOUNT SUBTOTAL
7 4410 1 NaN
8 4200 5 NaN
9 total 6 total
11 4250 0 NaN
12 4255 0 NaN
But when I Restart and Run All I get a Key Error.
I have to run practically the same line twice,
df['SUBTOTAL'] = df['SKU'].apply(lambda x: isinstance(x, str))
df['SUBTOTAL'] = df[df['SKU'].apply(lambda x: isinstance(x, str))]
How do I split mixed type DataFrame into two columns?
The end result should be,
SKU AMOUNT SUBTOTAL
7 4410 1 NaN
8 4200 5 NaN
9 NaN 6 total
11 4250 0 NaN
12 4255 0 NaN
Or I could add a new SKUb column and drop, rename, whatever.
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.
Can you show us your desired output?
– jpp
21 mins ago