pandas Dataframe.str.strip returns nan values

Viewed 606

I'm following a tutorial for a market basket analysis. all works well but I want to export the data to a csv with only text and numbers.

the output of the csv looks something like this

,antecedents,consequents,antecedent support,consequent support,support,confidence,lift,leverage,conviction
0,frozenset({'DOLLY GIRL LUNCH BOX'}),frozenset({'SPACEBOY LUNCH BOX'}),0.22,0.28,0.21,0.95,3.41,0.148,15.84
1,frozenset({'SPACEBOY LUNCH BOX'}),frozenset({'DOLLY GIRL LUNCH BOX'}),0.28,0.22,0.21,0.75,3.41,0.15,3.12

It should look like this

antecedents,consequents,antecedent support,consequent support,support,confidence,lift,leverage,conviction
    SPACEBOY LUNCH BOX,DOLLY GIRL LUNCH BOX,0.28,0.22,0.21,0.75,3.41,0.15,3.12
    DOLLY GIRL LUNCH BOX,SPACEBOY LUNCH BOX,0.22,0.28,0.21,0.96,3.41,0.1484,15.84

I want to get rid of the frozenset({}) parts of the text

Is this possible in pandas our should I deconstruct and reconstruct the dataframe with str.strip?

df['antecedents'] = df['antecedents'].str.strip('({})')
df['consequents'] = df['consequents'].str.strip('({})')
2 Answers

try via agg():

cols=['antecedents','consequents']
df[cols]=df[cols].astype(str).agg(lambda x:x.str.strip("frozenset({''})"),1)

OR

In 2 steps via str.strip():

df['antecedents'] = df['antecedents'].astype(str).str.strip("frozenset({''})")
df['consequents'] = df['consequents'].astype(str).str.strip("frozenset({''})")

OR

via apply():

cols=['antecedents','consequents']
df[cols]=df[cols].astype(str).apply(lambda x:x.str.strip("frozenset({''})"),1)   

You could try the following:

df['antecedents'] = df['antecedents'].str.replace("frozenset({''})", "")
df['consequents'] = df['consequents'].str.replace("frozenset({''})", "")
Related