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
时间: 2023-07-16 13:14:52 浏览: 110
Pandas库之DataFrame使用的学习笔记
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.
阅读全文