Data type conversion is the process of transforming a variable from one data type to another. In Python, you can convert variables from one data type to another using built-in conversion functions. Here are a few examples:
- Number conversion: You can convert an integer to a decimal number using the float() function.
a. Converting an integer to a decimal :
x = 5
y = float(x)
print(y)
# Result: 5.0
b. Converting a decimal number to an integer :
x = 5.0
y = int(x)
print(y)
#Result : 5
-
Converting strings: You can convert a string into a number using the int() or float() functions.
a. Converting a string to an integer :
x = "5"
y = int(x)
print(y)
#Result: 5
b. Converting a character string to a decimal number :
x = "5.0"
y = float(x)
print(y)
#Result: 5.0
c. Converting a number to a string :
x = 5
y = str(x)
print(y)
#Result: "5"
- Converting Boolean values: You can convert a Boolean value into a number using the int() function. True is converted to 1 and False is converted to 0. You can also convert a number to a Boolean value using the bool() function. Any non-zero number is converted to True and zero is converted to False.
a. Converting a Boolean value to a number :
x = True
y = int(x)
print(y)
#Result: 1
b. Converting a number to a Boolean value :
x = 0
y = bool(x)
print(y)
#Result: False
- Convert list to numpy array :
import numpy as np
list = [1, 2, 3, 4, 5]
array = np.array(list)
print(array) # Result: [1 2 3 4 5]
In this example, a list is created with integers. The list is then converted into a numpy array using the « np.array » function. The numpy array is then displayed using the « print » function.
- Converting a numpy array to a list :
import numpy as np
array = np.array([1, 2, 3, 4, 5])
list = array.tolist()
print(list) # Result: [1, 2, 3, 4, 5]
In this example, a numpy array is created with integers. The numpy array is then converted to a list using the « tolist » method. The list is then displayed using the « print » function.
- Converting a dataframe to a dictionary :
import pandas as pd
df = pd.DataFrame({'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35]})
dictionary = df.to_dict()
print(dictionary) # Result: {'name': {0: 'Alice', 1: 'Bob', 2: 'Charlie'}, 'age': {0: 25, 1: 30, 2: 35}}
In this example, a dataframe is created with information about people. The dataframe is then converted into a dictionary using the « to_dict » method. The dictionary is then displayed using the « print » function.
- Converting a dictionary into a dataframe :
import pandas as pd
dictionary = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35]}
df = pd.DataFrame(dictionary)
print(df) # Result: name age
- Converting a a numpy array to a dataframe:
import pandas as pd
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
df = pd.DataFrame(array, columns=['col1', 'col2', 'col3'])
print(df)
In this example, a numpy array is created with integers. The numpy array is then converted to a dataframe by specifying column names using the « pd.DataFrame() » function. The resulting dataframe contains the values from the numpy array with the specified column names.
- Converting a dataframe to a numpy array:
import pandas as pd
import numpy as np
df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [4, 5, 6]})
array = df.values
print(array)
In this example, a dataframe is created with integers. The dataframe is then converted to a numpy array using the « values » attribute. However, during this conversion, column names and index information are lost. The numpy array only contains the numerical values.
It’s important to note that when converting between data types, information such as column names and index information may be lost. In addition, converting decimal numbers to integers can result in a loss of precision. It is therefore important to understand the implications of conversion between data types and to use it with care to avoid loss of precision or data.