convert column to integer pandas code example

Example 1: column dataframe to int

df[[column_name]].astype(int)

Example 2: convert column to numeric pandas

# convert all columns of DataFrame
df = df.apply(pd.to_numeric) # convert all columns of DataFrame

# convert just columns "a" and "b"
df[["a", "b"]] = df[["a", "b"]].apply(pd.to_numeric)

Example 3: make string numeric pandas

df['DataFrame Column'] = df['DataFrame Column'].astype(int)

Example 4: convert pandas series from str to int

df['DataFrame Column'] = pd.to_numeric(df['DataFrame Column'])

Example 5: convert a pandas column to int

# convert Series
my_series = pd.to_numeric(my_series)

# convert column "a" of a DataFrame
df["a"] = pd.to_numeric(df["a"])

Example 6: set column datatype pandas

df = pd.read_csv("weather.tsv", sep="\t",  
                 dtype={'Day': str,'Wind':int64})
df.dtypes