Home > Mobile >  Find columns in Pandas DataFrame containing dicts
Find columns in Pandas DataFrame containing dicts

Time:07-16

I have a pandas DataFrame with several columns containing dicts. I am trying to identify columns that contain at least 1 dict.

import pandas as pd
import numpy as np 

df = pd.DataFrame({
                   'i': [0, 1, 2, 3],
                   'd': [np.nan, {'p':1}, {'q':2}, np.nan],
                   't': [np.nan, {'u':1}, {'v':2}, np.nan]
                 })


 # Iterate over cols to find dicts
 cdict = [i for i in df.columns if isinstance(df[i][0],dict)]
 cdict
 [] 
 
 

How do I find cols with dicts? Is there a solution to find cols with dicts without iterating over every cell / value of columns?

CodePudding user response:

You can do :

s = df.applymap(lambda x:isinstance(x, dict)).any()
dict_cols = s[s].index.tolist()

print(dict_cols)

['d', 't']

CodePudding user response:

We can apply over the columns although this still is iterating but making use of apply.

df.apply(lambda x: [any(isinstance(y, dict) for y in x)], axis=0)

EDIT: I think using applymap is more direct. However, we can use our boolean result to get the column names

any_dct = df.apply(lambda x: [any(isinstance(y, dict) for y in 
 x)], axis=0, result_type="expand")
df.iloc[:,any_dct.iloc[0,:].tolist()].columns.values
  • Related