Skip to content
Advertisement

Why does a string cause entire pandas DataFrame to be non-numerical?

If I create a pandas DataFrame using numerical values, this is reflected in the DataFrame. However, if the first element is a string, i.e. 'a', the entire DataFrame goes grey and all numbers in it are converted to strings, i.e. 3 becomes '3'. Why and how to retain datatype diversity?

JavaScript

The output is

enter image description here

Advertisement

Answer

First problem is is use np.asarray(['a','b',3] all data are converting to strings, objects.

JavaScript

If pass list get mixed data – numeric with strings:

JavaScript

But working with mixed values is problemtic, most numeric operations failed, so the best is avoid it.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement