<jupyter_start><jupyter_text>Change data types can follows- [Numpy Data Types](https://www.w3schools.com/python/numpy/numpy_data_types.asp)- [Python Data Types](https://www.w3schools.com/python/python_datatypes.asp)```Python has the following data types built-in by default, in these categories:Text Type: strNumeric Types: int, float, complexSequence Types: list, tuple, rangeMapping Type: dictSet Types: set, frozensetBoolean Type: boolBinary Types: bytes, bytearray, memoryview```- df = df.astype({"A": float, "C": str, 'G': bool}, errors = 'raise') errors = 'ignore'- df.H = df.H.astype(float)<jupyter_code>import pandas as pd 
import numpy as np

rows = 3
df = pd.DataFrame(np.random.randint(0,rows,size=(rows, 6)), columns=list('ABCDEF'))

df['G'] = np.zeros(3)
df.head(3)<jupyter_output><empty_output><jupyter_text>Change multiple columns<jupyter_code>df = df.astype({"A": float, "C": str, 'G': bool})


### Notice how all columns is bring over
df.dtypes<jupyter_output><empty_output><jupyter_text>Dealing with errors in conversion<jupyter_code>def convert(df):
    try:

        df.H = df.H.astype(int)

        print(df)

    except ValueError as error:
        
        print("Failed to convert to Int when have nan value")

    finally:
        print(df.H.dtypes)
df1 = df.copy()
df1["H"] = np.NaN

convert(df1)<jupyter_output>Failed to convert to Int when have nan value
float64<jupyter_text>Solution 1Replace nan values with valid values<jupyter_code>df2 = df.copy()
df2['H'] = np.NaN
df2.H = df2.H.fillna(-1)

convert(df2)<jupyter_output>A  B  C  D  E  F      G  H
0  2.0  2  1  1  2  1  False -1
1  2.0  2  0  1  2  1  False -1
2  0.0  0  1  1  1  0  False -1
int32<jupyter_text>Solution 2Suppress error with flag errors=`ignore`. Opposite errors=`raise`<jupyter_code>df3 = df.copy()

df3['H'] = np.NaN

try:

    df3.H = df3.H.astype(int, errors = 'ignore')

    print(df3)

except ValueError as error:
    
    print("Failed when have nan value")

df3.H.dtypes<jupyter_output>A  B  C  D  E  F      G   H
0  2.0  2  1  1  2  1  False NaN
1  2.0  2  0  1  2  1  False NaN
2  0.0  0  1  1  1  0  False NaN<jupyter_text>Solution 3 INT64 can handle Nan Value<jupyter_code>df4 = df.copy()

df4['H'] = np.NaN
df4.H = df4.H.astype(pd.Int64Dtype())

print(df)<jupyter_output>A  B  C  D  E  F      G
0  2.0  2  1  1  2  1  False
1  2.0  2  0  1  2  1  False
2  0.0  0  1  1  1  0  False
{'fraction_non_alphanumeric': 0.12536564981195153, 'fraction_numerical': 0.046803175929795236, 'mean_word_length': 3.3369565217391304, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '274595', 'license': 'MIT', 'n_tokens_mistral': 997, 'n_tokens_neox': 963, 'n_words': 275, 'path': 'notebooks/pandas/change_column_type.ipynb', 'repo_name': 'codenamewei/pydata-science-playground', 'size': '2393'}