Python Pandas: Fill Missing Values with 0
Assuming you have a Pandas DataFrame, you can use the 'fillna()' method to replace all missing values with 0. Here's the Python code:
import pandas as pd
# Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, None, 4], 'B': [None, 3, 5, None]})
# Replace missing values with 0
df.fillna(0, inplace=True)
# Print the dataframe
print(df)
Output:
A B
0 1 0
1 2 3
2 0 5
3 4 0
The 'fillna()' method replaces all missing values with the specified value (in this case, 0). The 'inplace=True' parameter modifies the original DataFrame instead of creating a new one.
原文地址: https://www.cveoy.top/t/topic/nTKV 著作权归作者所有。请勿转载和采集!