In Pandas, NaN stands for 'Not a Number' and is recognized as a floating point representation of missing data. Here are several ways to create NaN values in a Pandas DataFrame:
Using None in Python: Pandas automatically converts None to NaN in float columns.
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, 2, None], 'B': [4, None, 6]}) print(df) Using numpy.nan: You can use numpy.nan to create NaN values directly.
df = pd.DataFrame({'A': [1, 2, np.nan], 'B': [4, np.nan, 6]}) print(df) Using float('nan'): You can also create NaN values with the Python float function.
df = pd.DataFrame({'A': [1, 2, float('nan')], 'B': [4, float('nan'), 6]}) print(df) Using a dictionary with lists of unequal length: When creating a DataFrame from a dictionary, if some lists are shorter than others, Pandas fills in the missing values with NaN.
df = pd.DataFrame({'A': [1, 2], 'B': [4, 5, 6]}) print(df) Using pandas functions that create missing data: Functions like reindex can create rows or columns with NaN where indices do not match.
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) df_reindexed = df.reindex([0, 1, 2, 3]) print(df_reindexed) Using pandas.read_csv or similar functions: When reading data from a file, NaN values can be introduced for missing entries or can be specified via the na_values parameter.
# Consider a CSV with missing values: # A,B # 1,4 # 2, # ,6 df = pd.read_csv('path_to_csv.csv') print(df) Using .replace(): You can replace existing values with NaN.
df = pd.DataFrame({'A': [1, 2, -999], 'B': [4, -999, 6]}) df.replace(-999, np.nan, inplace=True) print(df) Using .where() or .mask(): You can conditionally replace values with NaN.
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) df.where(df > 2, np.nan, inplace=True) print(df) In all cases, the resulting DataFrame will have NaN values in the positions specified or created by the method used. Remember that NaN is a floating point value, so using it will generally convert the data type of the column to float64.
select-options android-radiogroup influxdb nsmutablearray alamofire space mobile-development jxl aws-cloudformation linear-algebra