All I have successfully written a list comprehension that tests for non ascii characters in a column in a dataframe.
I am trying now to write a nest listed comprehension to check all of the columns in the data frame.
I have researched this by searching nested List Comprehensions dataframes and several other variations and while they are close I can get them to fit my problem.
Here is my code:
import pandas as pd
import numpy as np
data = {'X1': ['A', 'B', 'C', 'D', 'E'],
'X2': ['meow', 'bark', 'moo', 'squeak', '120°']}
data2 = {'X1': ['A', 'B', 'F', 'D', 'E'],
'X3': ['cat', 'dog', 'frog', 'mouse®', 'chick']}
df = pd.DataFrame(data)
df2 = pd.DataFrame(data2)
dfAsc = pd.merge(df, df2, how ='inner', on = 'X1')
dfAsc['X2']=[row.encode('ascii', 'ignore').decode('ascii') for row in
dfAsc['X2'] if type(row) is str]
dfAsc
which correctly returns:
X1 X2 X3
0 A meow cat
1 B bark dog
2 D squeak mouse®
3 E 120 chick
I have tried to create a nested comprehension to check all of the columns instead of just X2. The attempt below is to create a new df that contains the answer. If this continues to be an issue of confusion, I'll will delete it as it is only one of my attempts to obtain the answer, don't get hung up on it please
df3 = pd.DataFrame([dfAsc.loc[idx]
for idx in dfAsc.index
[row.encode('ascii', 'ignore').decode('ascii') for row in
dfAsc[idx] if type(row) is str]
df3
which doesnt work. I know Im close but Im still having trouble getting my head around comprehensions