I have a list of data frame columns. I need to check those names with the data frame column names if it matches then it should place the d-type names into one list and d-type object names into another list I have tried but can't able to convert them into lists. How do I do it?
Input:
col_lst = ['bc','fgc ','drt']
df=
abc bc fgc drt
0 0 q d 3
1 0 g t 1
2 0 a g 4
3 0 d c 5
4 0 h b 6
My code:
x=[]
for col in df:
for i in col_lst:
if i in col:
if df[col].dtype == float or df[col].dtype == int:
c_int=[]
for i in col: c_int.append(list(i.split(","))
elif df[col].dtype == object:
c_obj=[]
for i in col: c_obj.append(list(i.split(","))
the output Iam getting:
c_int = [['d'],['r'],['t']]
c_obj=
[['b'], ['c']]
[['f'],['g'],['c']]
the output I need:
c_int =['drt']
c_obj =['bc','fgc']
Here it is.
col_lst = ['bc','fgc','drt']
df = pd.DataFrame({'abc':[0,0,0,0,0], 'bc':['q','g','a','d','h'], 'fgc':['d','t','g','c','b'], 'drt':[3,1,4,5,6]})
common = set(col_lst).intersection(set(df.columns))
c_int=[]
c_obj = []
for col in common:
if df[col].dtype==float or df[col].dtype == int:
c_int.append(col)
elif df[col].dtype == object:
c_obj.append((col))
print(c_int)
print(c_obj)
prints:
['drt']
['fgc', 'bc']