我有一个包含10列的pandas数据框,我希望为除1之外的所有列填充缺失值(假设该列称为test
)。目前,如果我这样做:
df.fillna(df.median(), inplace=True)
它用中值替换所有列中的NA值,如何在不指定所有其他列的情况下排除特定列
答案 0 :(得分:4)
您可以使用pd.DataFrame.drop
帮助
df.drop('unwanted_column', 1).fillna(df.median())
或pd.Index.difference
df.loc[:, df.columns.difference(['unwanted_column'])].fillna(df.median())
或者只是
df.loc[:, df.columns != 'unwanted_column']
输入差异函数应作为数组传递(编辑)。
答案 1 :(得分:3)
只需使用pandas的列索引选择您想要的任何列:
>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({'A': [np.nan, 5, 2, np.nan, 3], 'B': [np.nan, 4, 3, 5, np.nan], 'C': [np.nan, 4, 3, 2, 1]})
>>> df
A B C
0 NaN NaN NaN
1 5.0 4.0 4.0
2 2.0 3.0 3.0
3 NaN 5.0 2.0
4 3.0 NaN 1.0
>>> cols = ['A', 'B']
>>> df[cols] = df[cols].fillna(df[cols].median())
>>> df
A B C
0 3.0 4.0 NaN
1 5.0 4.0 4.0
2 2.0 3.0 3.0
3 3.0 5.0 2.0
4 3.0 4.0 1.0
答案 2 :(得分:0)
exports.dbWriteOnNewPost = functions.database.ref('/posts/{postid}').onWrite((change, context) => {
const postUUID = context.params.postid;
const postData = change.after.val();
const communityUUID = postData.community;
const authorUUID = postData.author;
const postDate = postData.date;
const promisePostByCommunity = admin.database().ref('/posts_by_community/' + communityUUID + '/' + postUUID).set(postDate);
const promisePostByUser = admin.database().ref('/posts_by_user/' + authorUUID + '/' + postUUID).set(postDate);
const promiseCommunityPostsCount = admin.database().ref('/communities/' + communityUUID + '/posts_count').transaction(
(posts_value) => {
return posts_value + 1;
}
);
return Promise.all([promisePostByCommunity, promisePostByUser, promiseCommunityPostsCount]);
});
这将用字符串“ NO”替换data_rnr ['CO BORROWER NAME']列下的“ NA”值。