I have a DataFrame
like this;
df = pd.DataFrame([
['A1', ['Long Sleeves', 'Jacket'], 85],
['B1', 'Shoes', 55],
['A2', 'Skirt', 40]
], columns=['PRDS_ID', 'CAT', 'PRICE'])
I want to select rows that the type is list
from column 'CAT'.
Therefore, I tried following codes;
df[df.CAT.astype(list) == True]
df['CAT'].apply(lambda x: len(x) > 1)
but neither of them worked. The first row should be returned in this case. How can I fix it?
Is that what you mean?
df['CAT'].apply(lambda x: x if isinstance(x, list) else None)
0 [Long Sleeves, Jacket]
1 None
2 None
Name: CAT, dtype: object
You can also use apply
like
df[df.CAT.apply(type) == list]
PRDS_ID CAT PRICE
0 A1 [Long Sleeves, Jacket] 85
df.CAT.apply(type)
Will give you Series
of object types of each rows then df.CAT.apply(type) == list
we can get booleans to do indexingPANDAS cells do not have an istype
method; that's for Python base objects and others that implement the operator. Instead, you have to wrap your check inside a lambda function that you apply to the contents of each cell: you refer to the Python object inside the cell, and test that .
>>> df[df.iloc[:,1].map(lambda x: type(x) == list)]
PRDS_ID CAT PRICE
0 A1 [Long Sleeves, Jacket] 85
>>> df[df.iloc[:,1].map(lambda x: type(x) == str)]
PRDS_ID CAT PRICE
1 B1 Shoes 55
2 A2 Skirt 40
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.