在熊猫数据框中找到非NAN元素的总数

时间:2019-11-04 23:07:09

标签: python pandas

我知道df.count()和df.groupby.count(),但是我只是想要数据帧中特定列(例如称为“汽车”)的非NAN元素的数量。

我知道df.size [0],但是此命令不尊重非NAN元素的数量在不同列中可能不同的事实。

2 个答案:

答案 0 :(得分:3)

还有# download input images to disk input_images = @user.photos.first(25) .map { |photo| photo.image[:thumb] } .map(&:download) # create temporary file for output image processed_image = Tempfile.new ["montage", ".jpg"], binmode: true MiniMagick::Tool::Montage.new do |montage| montage.merge! input_images.map(&:path) montage.tile "5x5" montage << processed_image.path end attachments.inline['images.jpg'] = File.binread(processed_image.path) # close and delete temporary files [*input_images, processed_image].each(&:close!) 系列。它还忽略count。来自文档

NaN

因此,对于列s = pd.Series([0.0, 1.0, np.nan]) s.count() Out[307]: 2

cars

将返回列df['cars'].count() 的Non-NaN值的数量

答案 1 :(得分:1)

我们还可以使用Series.notna + Series.sum

df.cars.notna().sum()