4

Is it possible to create a sparse Pandas DataFrame that has columns both containing floats and strings? I.e, I have a dataframe:

df2 = pd.DataFrame({'A':[0., 1., 2., 0.], 
                    'B': ['a','b','c','d']}, columns=['A','B'])

And I want to convert this to a sparse dataframe, but df2.to_sparse(fill_value=0.) gives:

ValueError: could not convert string to float: d

Is there any way to make this work?

1 Answer 1

1

What you could do is map your strings to ints/floats and map your column B to their dict lookup values into a new column C and then create the sparse dataframe like so:

temp={}
# we want just the unique values here for the dict
for x in enumerate(df2['B'].unique().tolist()):
    val, key = x
    temp[key]=val
temp

Out[106]:
{'a': 0, 'b': 1, 'c': 2, 'd': 3}

# now add this column

In [108]:

df2['C']=df2['B'].map(temp)
df2
Out[108]:
   A  B  C
0  0  a  0
1  1  b  1
2  2  c  2
3  0  d  3

# now pass the two columns to create the sparse matrix:

In [109]:

df2[['A', 'C',]].to_sparse(fill_value=0)
Out[109]:
   A  C
0  0  0
1  1  1
2  2  2
3  0  3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.