Write a program to fill missing values (NaN) in a DataFrame using fillna().
时间: 2024-10-11 19:03:17 浏览: 25
在Python的pandas库中,`fillna()`函数是一个非常实用的方法,用于填充DataFrame中的缺失值(NaN)。这是一个简单的例子,展示如何编写一个程序来处理这个问题:
```python
import pandas as pd
# 假设我们有一个包含缺失值的DataFrame df
data = {
'A': [1, 2, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': [9, 10, 11, 12]
}
df = pd.DataFrame(data)
# 使用fillna()函数替换缺失值
# 可以选择直接填入特定值(如0或某个列的平均值)
# 或者使用前一个非缺失值(ffill)或下一个非缺失值(bfill)
df_filled = df.fillna(0) # 指定默认值0
# 或者使用向前填充(ffill)
df_ffill = df.fillna(method='ffill')
# 或者使用向后填充(bfill)
df_bfill = df.fillna(method='bfill')
# 输出处理后的数据框
print("Filled with zeros:")
print(df_filled)
print("\nForward-filled:")
print(df_ffill)
print("\nBackward-filled:")
print(df_bfill)
相关问题
Write a function named fill_na_row that takes a pandas DataFrame as input and returns a NaN filled DataFrame, where any NaN values are replaced by corresponding row means (average of each row without NaN values). Note that the input should not be modified and give an example
Here's the function `fill_na_row`:
```python
import pandas as pd
import numpy as np
def fill_na_row(df):
df_copy = df.copy()
for i, row in df_copy.iterrows():
row_mean = np.nanmean(row)
df_copy.iloc[i] = row.fillna(row_mean)
return df_copy
```
This function creates a copy of the input DataFrame to avoid modifying it. Then, it iterates over each row of the DataFrame. For each row, it calculates the mean of the values that are not NaN using `np.nanmean()`. It then replaces any NaN values in the row with this mean value using the `fillna()` method. Finally, it returns the filled DataFrame.
Here's an example of how to use this function:
```python
# Create example DataFrame
df = pd.DataFrame({'A': [1, np.nan, 3, 4],
'B': [5, 6, np.nan, 8],
'C': [9, 10, 11, np.nan]})
# Call fill_na_row on the DataFrame
filled_df = fill_na_row(df)
# Print the original DataFrame
print("Original DataFrame:")
print(df)
# Print the filled DataFrame
print("\nFilled DataFrame:")
print(filled_df)
```
Output:
```
Original DataFrame:
A B C
0 1.0 5.0 9.0
1 NaN 6.0 10.0
2 3.0 NaN 11.0
3 4.0 8.0 NaN
Filled DataFrame:
A B C
0 1.0 5.0 9.0
1 8.0 6.0 10.0
2 3.0 7.0 11.0
3 4.0 8.0 6.0
```
As we can see, the NaN values in the original DataFrame have been replaced by the corresponding row means in the filled DataFrame.
dataframe fillna 示例
好的,下面是一个 dataframe fillna 的示例代码:
```python
import pandas as pd
import numpy as np
# 创建包含空值的 DataFrame
df = pd.DataFrame({'A': [1, 2, np.nan, 4],
'B': [5, np.nan, 7, 8],
'C': [9, 10, 11, np.nan]})
# 使用 0 填充空值
df.fillna(0)
# 输出结果
A B C
0 1.0 5.0 9.0
1 2.0 0.0 10.0
2 0.0 7.0 11.0
3 4.0 8.0 0.0
```
在这个示例中,我们使用 `fillna` 方法来将 DataFrame 中的空值填充为 0。可以看到,原来包含空值的 DataFrame 现在已经被填充为了一个完整的 DataFrame。
阅读全文