I am having a hard time iterating a function over a list of dataframes, I would like to create another list dataframes appying the repr() function over each element in a specific column of every dataframe in a nested list in order to get a sense of the elemnts that have hidden control characters. I have a list of dataframes like this:
df_list = [df1, df2, df3]
And every dataframe in the list looks like this:
df1: df2: df3:
ID REG ID REG ID REG
aad 03158 aee 05268 sse 05214
eed 02545 fed 09679 fbh 02684
xxe 01256 hyd 04784 ghu 03689
So I am trying to use the repr() function in a loop in order to get the following result:
df_list2 = [df4, df5, df6]
Where every resulting dataframe should be:
df4: df5: df6:
REG REG REG
03158\r\r\n 05268 05214\r\r\n
02545\r\r\n 09679 02684
01256 04784\r\r\n 03689\r\r\n
I tried using a loop like this:
df_list2 = []
for datafr in df_list:
for element in datafr[]:
df_list2[].append(repr(elemnt))
And I got a flat list instead:
df_list2 = ['03158\r\r\n', '05268', '05214\r\r\n', '02545\r\r\n', '09679', '02684', '01256', '04784\r\r\n', '03689\r\r\n']
Any hints about how I can fix this?, I read many other answers about the topic but I couldn't get any idea on how to approach the problem, I am a begginer in Python so I thought I would be a good idea to ask here.
CodePudding user response:
You can use apply() for each dataframe in df_list
df_list2 = []
for datafr in df_list:
df_list2.append(pd.DataFrame({'REG': datafr['REG'].apply(repr)}))