简体   繁体   中英

Why does this code yield a NaN in my pandas dataframe?

I'm trying to write some code to predict outcomes of a sports season. I have a dataframe with an int32 Year column, an object Team column for listing the names of the teams, an int32 Capacity column with stadium capacities, and an int32 Attend/G column with average attendance per game played in a given year. There are no previous NaNs within the data.

This line of code perfectly overwrites the stadium capacity for team NYM before a given year

train.loc[(train.Year < 2009) & (train.Team == 'NYM'), 'Capacity'] = 57333

However, the code shown below fails to do so, as it converts the capacity for team PHI to a NaN, and the capacities for the other teams to floats.

I assigned a variable to store the highest Attend/G number for a team. The variable is an int32.

max_attend_per_g_phi = (train.loc[train['Team'] =='PHI',['Attend/G']].max())
print(max_attend_per_g_phi)
print(train.info()
train.loc[(train.Year < 2012) & (train.Team=='PHI'),'Capacity']=max_attend_per_g_phi
print(train.info())

The first train.info call outputs

Attend/G        360 non-null int32
Capacity        360 non-null int32
Team            360 non-null object
Year            360 non-null int32

While the second call outputs

Attend/G        360 non-null int32
Capacity        355 non-null float64
Team            360 non-null object
Year            360 non-null int32

Overwriting the stadium capacity with that variable in the same format as the first line of code yields a NaN for the PHI capacity, as well as converts the other capacities in the dataframe from ints to floats.

Does anyone know why this is occurring?

In some cases working with Pandas for instance when read from CSV pd.read_csv(na_values = "?") you may check your data afterwords like this:

dataset.isna().sum()

This may bring the feedback if your data has the NaN inside the original document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM