【问题标题】:identify data with zero value in python在python中识别零值的数据
【发布时间】:2020-05-06 11:47:20
【问题描述】:

我有以下 csv 格式的数据

Date,State,City,Station Code,Minimum temperature (C),Maximum temperature (C),Rainfall (mm),Evaporation (mm),Sunshine (hours),Direction of maximum wind gust,Speed of maximum wind gust (km/h),9am Temperature (C),9am relative humidity (%),3pm Temperature (C),3pm relative humidity (%)
2017-12-25,VIC,Melbourne,086338,15.1,21.4,0,8.2,10.4,S,44,17.2,57,20.7,54
2017-12-25,VIC,Bendigo,081123,11.3,26.3,0,,,ESE,46,17.2,53,25.5,25
2017-12-25,QLD,Gold Coast,040764,22.3,35.7,0,,,SE,59,29.2,53,27.7,67
2017-12-25,SA,Adelaide,023034,13.9,29.5,0,10.8,12.4,N,43,18.6,42,27.7,17

VIC 的输出应该是

S : 1
ESE : 1
SE : 0
N : 0

但是我得到的输出是

S : 1
ESE : 1

因此想知道,如何使用唯一函数来包含其他 2 个缺失的结果。下面是调用 csv 文件的程序

import pandas as pd
#read file
df = pd.read_csv('climate_data_Dec2017.csv')

#marker
value = df['Date']
date = value == "2017-12-26"
marker = df[date]

#group data
directionwise_data = marker.groupby('Direction of maximum wind gust')
count = directionwise_data.size()
numbers = count.to_dict()

for key in numbers:
  print(key, ":", numbers[key])

【问题讨论】:

  • 我真的很难理解你在这里想要做什么!特别是您的代码对您提供的数据样本没有产生任何结果。您能否进一步详细说明您正在尝试做什么?

标签: python pandas unique


【解决方案1】:

首先,我不确定您想从中得到什么: 您的数据样本没有“2017-12-26”记录但您在代码中使用它,因此我假设该样本,我将代码更改为“2017-12-25”只是为了看看它是什么生产,现在它产生了你所期望的确切的东西!因此,我猜在您的完整数据中,您没有 SE 和 N 的“2017-12-26”记录,因此它没有被分组,我建议您创建一个独特的集合,其中包含您在 df 中的四个方向,然后只需在所需日期的数据框中计算它们的出现次数!

或者,如果您只想按日期为每个方向记录多少条记录,为什么不像下面这样旋转它:

output = df.pivot_table(index='Date', columns = 'Direction of maximum wind gust', aggfunc={'Direction of maximum wind gust':'count'}, fill_value=0)

编辑: 好的,所以我写了这篇非常快的文章,它应该可以得到你想要的,但是你需要给它你想要的日期:

import pandas as pd

#read csv
df = pd.read_csv('climate_data_Dec2017.csv')
#specify date
neededDate = '2017-12-25'
#slice dataframe to keep needed records based on the date
subFrame = df.loc[df['Date'] == neededDate].reset_index(drop=True)
#set count to zero
d1 = 0 #'S'
d2 = 0 #'SE'
d3 = 0 #'N'
d4 = 0 #'ESE'
#loop over slice and count directions
for i, row in subFrame.iterrows():
    direction = subFrame.at[i,'Direction of maximum wind gust']
    if direction == 'S':
        d1 = d1+1
    elif direction == 'SE':
        d2 = d2+1
    elif direction == 'N':
        d3 = d3+1
    if direction == 'ESE':
        d4 = d4+1
#print directions count
print ('S = ' + str(d1))
print ('SE = ' + str(d2))
print ('N = ' + str(d3))
print ('ESE = ' + str(d4))

S = 1
SE = 1
N = 1
ESE = 1

【讨论】:

    猜你喜欢
    • 2021-12-10
    • 2021-02-09
    • 1970-01-01
    • 2014-11-27
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    相关资源
    最近更新 更多