0 votes
1 view
in Python by (15.7k points)

I want to convert a table, represented as a list of lists, into a Pandas DataFrame. As an extremely simplified example:

a = [['a', '1.2', '4.2'], ['b', '70', '0.03'], ['x', '5', '0']] 

df = pd.DataFrame(a)

What is the best way to convert the columns to the appropriate types, in this case, columns 2 and 3 into floats? Is there a way to specify the types while converting to DataFrame? Or is it better to create the DataFrame first and then loop through the columns to change the type for each column? Ideally, I would like to do this in a dynamic way because there can be hundreds of columns and I don't want to specify exactly which columns are of which type. All I can guarantee is that each column contains values of the same type.

1 Answer

0 votes
by (107k points)
  • There are many ways to change the datatype of a column in Pandas. Some of them are as follows:-

  • to_numeric():-

  • This is the best way to convert one or more columns of a DataFrame to numeric values is to use pandas.to_numeric() method to do the conversion.

  • This function will try to change non-numeric objects (such as strings) into integers or floating point numbers.

  • This piece of code converts all columns of DataFrame 

df = df.apply(pd.to_numeric) 

  • The below-mentioned code will convert just columns "a" and "b" 

df[["a", "b"]] = df[["a", "b"]].apply(pd.to_numeric)

  •  astype():-

  • This method enables you to be explicit about the data type you want to have. It's very versatile i.eo you can try and go from one type to any other.

  • Below are some of the examples of different conversion using astype() method:-

  • convert all DataFrame columns to the int64 dtype

 df = df.astype(int)

  • convert column "a" to int64 dtype and "b" to complex 

type df = df.astype({"a": int, "b": complex}) 

  • convert Series to float16 type 

s = s.astype(np.float16) 

  • convert Series to Python strings

     s = s.astype(str)

  • convert Series to categorical type - see docs for more details 

s = s.astype('category')

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...