Find Values In Indexed Columns Of Pandas Which Is A List
Let's say I have a dataframe like following d = {'col1': [1, 2,4,5,6], 'col2': [3, 4,5,6,7], 'col3': ['foo', 'bar', 'baz','biz', 'boo'], 'col4': [['foo', 'bar'], ['baz'], ['foo
Solution 1:
Use Index.map
with in
statement:
df1 = indexed_df[indexed_df.index.map(lambda x: 'baz'in x)]
print (df1)
col1 col2 col3
col4
[baz] 24 bar
[baz, foo] 67 boo
Alternative with list comprehension:
df1 = indexed_df[['baz' in x for x in indexed_df.index]]
Solution 2:
Let us just try explode
out= indexed_df[pd.Series(indexed_df.index).explode().isin(['baz']).any(level=0).values]
Out[241]:
col1 col2 col3
col4
[baz] 24 bar
[baz, foo] 67 boo
Solution 3:
You can take advantage of using MultiIndex
rather than a list as index:
>>> df.set_index(pd.MultiIndex.from_tuples(d["col4"]))
col1 col2 col3 col4
foo bar 13 foo [foo, bar]
baz NaN 24 bar [baz]
foo NaN 45 baz [foo]
biz NaN 56 biz [biz]
baz foo 67 boo [baz, foo]
Post a Comment for "Find Values In Indexed Columns Of Pandas Which Is A List"