[英]how to replace empty series values with NaN in python
我遍歷許多列,並將它們的摘要統計量(例如均值,中位數,偏度和峰度)存儲在以下字典中:
metrics_dict['skewness'] = data_col.skew().values[0]
metrics_dict['kurtosis'] = data_col.kurt().values[0]
metrics_dict['mean'] = np.mean(data_col)[0]
metrics_dict['median'] = np.median(data_col)
但是對於某些列,它給出如下錯誤:
IndexError: index out of bounds
有問題的列如下:
Index device
61021 C:2
61022 D:3+
61023 D:3+
61024 B:1
61025 D:3+
61026 C:2
我只想在這樣的列的情況下將NA附加到字典上,而不會讓錯誤中斷我的循環。 這里的index只是數據幀的索引,操作中的列是device。 請注意,數據中有大量的數字列(〜500),其中2 -3列就像設備,因此我只需要為這些字典添加NA並移至下一列。 有人可以告訴我如何使用python嗎?
由於這些統計信息僅對數字列有意義,因此您可以嘗試隔離數字列。 使用pd.DataFrame.select_dtypes
可以pd.DataFrame.select_dtypes
:
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
numeric_cols = df.select_dtypes(include=numerics).columns
for col in df:
if col in numeric_cols:
# calculate & add some values to dictionary
else:
# add NA values to dictionary
您可以嘗試使用try / except IndexError
try:
# whatever you do that might rise an IndexError
except IndexError:
# append NA to dict
選擇要在其中將空值設置為nan的數據框列。
df[df['col'] == ''] = np.nan
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.