I'm using Pandas to transform some sporting data. One column is the home team stats and the 2nd column is away team stats.
The stats are read from an excel file. When i print a dictionary from the dataframe all of the away team stats are floats (but many should be integers). When I print the type of each column values the first column will show up as Integers and floats while all of the 2nd column consists of numpy.float64 values.
How can I get both columns to be integers and float values?
Here is the python script and output:
import pandas as pd
import numpy as np
pd.options.mode.chained_assignment = None # Remove warning. default='warn'
teams_df = pd.read_excel("STATS.xlsm", skiprows=8, nrows=12, usecols=[0,2])
new_teams_df = teams_df.rename(columns={"Unnamed: 0": "HOME", "Unnamed: 2": "AWAY"})
new_teams_df = new_teams_df.dropna()
print("\n********************\n Data Frame as dict \n********************")
print(new_teams_df.to_dict())
print("\nHome Column Row 1 Type: " + str(type(new_teams_df.at[1,'HOME'])))
print("Away Column Row 1 Type: " + str(type(new_teams_df.at[1,'AWAY'])))
print("\nHome Column Row 10 Type: " + str(type(new_teams_df.at[10,'HOME'])))
print("Away Column Row 10 Type: " + str(type(new_teams_df.at[10,'AWAY'])))
Outputs
********************
Data Frame as dict
********************
{'HOME': {0: 342, 1: 232, 2: 110, 3: 23, 4: 27, 7: 23, 8: 0.5652, 9: 26.3, 10: 14.9, 11: 44}, 'AWAY': {0: 339.0, 1: 214.0, 2: 125.0, 3: 45.0, 4: 25.0, 7: 18.0, 8: 0.5, 9: 37.7, 10: 18.8, 11: 43.0}}
Home Column Row 1 Type: <class 'int'>
Away Column Row 1 Type: <class 'numpy.float64'>
Home Column Row 10 Type: <class 'float'>
Away Column Row 10 Type: <class 'numpy.float64'>
Strange issue because the data is coming straight from a stats website onto an Excel file. Both columns should be exactly the same. Is there a away to convert the away column back to python objects. Some rows would need to be floats and the rest integers.
Thanks!
numpy.float64is just an alias for a Pythonfloat.df['col_name'] = df['col_name'].astype('float')