使用R、Python或EXCEL查找风速最高和最低的一天

使用R、Python或EXCEL查找风速最高和最低的一天,python,r,excel,Python,R,Excel,我有一个excel文件,其中有31个选项卡,对应于5月份的一天。每个选项卡或工作表有3列(高度、速度、方向) 我想找到风速最大的一天。我尝试使用excel的函数MAX=MAX(wind1:wind31!C1:C17)来查找它,但它只给出了一个最大值。有没有办法确定一天的风速是整个月最高的,而不仅仅是一个最大值,因为高度起着作用。我必须做一些统计杂耍(请原谅行话) 我有R软件和Python,但我基本上是个新手 这些数据来自31张图纸中的3张 Day 1

我有一个excel文件,其中有31个选项卡,对应于5月份的一天。每个选项卡或工作表有3列(高度、速度、方向)

我想找到风速最大的一天。我尝试使用excel的函数
MAX=MAX(wind1:wind31!C1:C17)
来查找它,但它只给出了一个最大值。有没有办法确定一天的风速是整个月最高的,而不仅仅是一个最大值,因为高度起着作用。我必须做一些统计杂耍(请原谅行话)

我有R软件和Python,但我基本上是个新手

这些数据来自31张图纸中的3张

        Day 1               Day 2               Day 3    and so on
Height  Dir Spd     Height  Dir Spd     Height  Dir Spd
139     333 6.5     110     254 3.6     157     341 6.9
790     343 5.9     767     264 4.3     814     357 6.2
1492    343 5.7     1471    274 6.6     1522    0   5.6
3079    297 9.4     3061    284 14.9    3127    317 10.3
4311    293 19      4291    289 21.9    4375    309 14.9
5731    291 28.6    5706    292 30.4    5809    306 19.1
7406    288 38.7    7381    294 42.8    7498    299 22.4
9462    286 47.6    9440    294 56      9550    290 22.5
10694   285 47.9    10679   293 61      10777   288 22.4
12129   281 46.9    12130   296 60.6    12207   292 23.8
13940   279 33.8    13936   296 40.4    13994   282 25.4
16473   279 13.8    16464   282 13.7    16517   286 11.7
18673   278 3       18665   324 2.9     18716   323 2.6
20786   63  2.3     20775   61  2.9     20824   59  4.1
24036   100 6       24015   104 4.4     24072   96  6.9
26676   85  5.5     26656   73  4       26719   83  7.9
31287   103 6.9     31253   102 7.9     31335   101 10.2

如果将数据转换为如下连续格式:

Day Height  Dir Spd
1   139    333  6.5
1   790    343  5.9
1   1492   343  5.7
.   .      .    .
.   .      .    .
.   .      .    .
2   110    254  3.6
2   767    264  4.3
.   .      .    .
.   .      .    .
31  26719   83  7.9
31  31335  101  10.2
您只需在Excel
OFFSET(A1,MATCH(MAX(Spd),Spd,0),0)中使用此公式即可
其中单元格
A1
位于网格的左上角,包含单词
Day
Max(Spd)
是整个
Spd
列的最大值<代码>偏移量和
匹配
是Excel函数

另一种解决方案是在每张表中命名
Spd
数据的范围,例如每天的
Spd_1
Spd_2
,等等。Excel函数
MAX(间接(“Spd_1”))
MAX(间接(“Spd_2”))
等可用于单个工作表中以字符串表示的命名范围。然后可以使用单个
max
函数查找相应的日期

如果可以在
R
中加载与数据帧相同的数据,则可以执行以下操作
subset(df,Spd==max(df[,“Spd”])$Day
其中
df
是通过
read.csv
read.table
或类似方式读入的数据帧的名称

以上两项都可以用
min
代替
max
重复,以找到最低速度

如果您无法将其转换为该格式,或者无法使用Excel的
间接
,那么最好的解决方案是在Excel中使用简单的VBA在工作表中循环


在任何情况下,你都可能需要考虑如何处理关系,比如在两天或两天以上的时间里,以相同(最大)的速度处理关系。

如果你能接受R为重复的列名创建唯一的列名,你就不必把一天的时间浪费在单个列名上(这篇文章的内容有点多)然后,您可以删除“Day”标题行,将月份的读数列保留在一起,就像上面一样,并将其生成一个CSV,R可以使用
read.CSV()
读取

这是从上面的数据片段中读取的R数据帧结构:

dat <- structure(list(Height = c(139L, 790L, 1492L, 3079L, 4311L, 5731L, 
        7406L, 9462L, 10694L, 12129L, 13940L, 16473L, 18673L, 20786L, 
        24036L, 26676L, 31287L), Dir = c(333L, 343L, 343L, 297L, 293L, 
        291L, 288L, 286L, 285L, 281L, 279L, 279L, 278L, 63L, 100L, 85L, 
        103L), Spd = c(6.5, 5.9, 5.7, 9.4, 19, 28.6, 38.7, 47.6, 47.9, 
        46.9, 33.8, 13.8, 3, 2.3, 6, 5.5, 6.9), Height.1 = c(110L, 767L, 
        1471L, 3061L, 4291L, 5706L, 7381L, 9440L, 10679L, 12130L, 13936L, 
        16464L, 18665L, 20775L, 24015L, 26656L, 31253L), Dir.1 = c(254L, 
        264L, 274L, 284L, 289L, 292L, 294L, 294L, 293L, 296L, 296L, 282L, 
        324L, 61L, 104L, 73L, 102L), Spd.1 = c(3.6, 4.3, 6.6, 14.9, 21.9, 
        30.4, 42.8, 56, 61, 60.6, 40.4, 13.7, 2.9, 2.9, 4.4, 4, 7.9), 
            Height.2 = c(157L, 814L, 1522L, 3127L, 4375L, 5809L, 7498L, 
            9550L, 10777L, 12207L, 13994L, 16517L, 18716L, 20824L, 24072L, 
            26719L, 31335L), Dir.2 = c(341L, 357L, 0L, 317L, 309L, 306L, 
            299L, 290L, 288L, 292L, 282L, 286L, 323L, 59L, 96L, 83L, 
            101L), Spd.2 = c(6.9, 6.2, 5.6, 10.3, 14.9, 19.1, 22.4, 22.5, 
            22.4, 23.8, 25.4, 11.7, 2.6, 4.1, 6.9, 7.9, 10.2)), .Names = c("Height", 
        "Dir", "Spd", "Height.1", "Dir.1", "Spd.1", "Height.2", "Dir.2", 
        "Spd.2"), class = "data.frame", row.names = c(NA, -17L))
要获取整个数据帧的最大速度值的列名,我们首先需要处理“Spd”列:

# only work with "Spd" columns

tmp <- dat[,which(grepl("Spd", names(dat)))]

# showing what we have left

str(tmp)

## 'data.frame':    17 obs. of  3 variables:
##  $ Spd  : num  6.5 5.9 5.7 9.4 19 28.6 38.7 47.6 47.9 46.9 ...
##  $ Spd.1: num  3.6 4.3 6.6 14.9 21.9 30.4 42.8 56 61 60.6 ...
##  $ Spd.2: num  6.9 6.2 5.6 10.3 14.9 19.1 22.4 22.5 22.4 23.8 ...
但是我们只希望列具有总的最大值,因此我们将把
apply
输入
which.max

# which one of those has the max value (returns name & position)
which.max(apply(tmp, 2, max))

## Spd.1 
##     2 
并保留列名/#和最大值

所有这些都可以在一行可怕的、不可读的行中完成:

which.max(apply(dat[, which(grepl("Spd", names(dat)))], 2, max))

我只是想说明它并不像解释中所说的那样复杂。

Python和pandas模块是一种可能的解决方案:

#! /usr/bin/env python      
import pandas as pd

# Export the tabs as csv-files: day1.csv, day2.csv, ..., day31.csv.
# Assume the first line is a header line and that columns are
# separated by ',':
#
# Height ,  Dir , Spd
# 139    ,  333 , 6.5
# 790    ,  343 , 5.9
# ...
#

# Use or own column names and skip header.
column_names = ['height', 'direction',  'speed']

# Read in the data for each day.
alldays = []
for d in range(1, 32):
    fname = "day{}.csv".format(d)
    frame = pd.read_csv(fname, names=column_names, header=0)
    frame['day'] = d
    alldays.append(frame)

# Concatenate all days into DataFrame.
data = pd.concat(alldays, ignore_index=True)

# Get index for max and use it to retrieve the day and the speed.
idx_max = data.speed.idxmax()
max_row = data.ix[idx_max]
print("Maximum wind speed {} on day {}".format(max_row.speed, int(max_row.day)))

# Same as above but for the minimum.
idx_min = data.speed.idxmin()
min_row = data.ix[idx_min]
print("Minimum wind speed {} on day {}".format(min_row.speed, int(min_row.day)))
将此保存为脚本
highlow.py
。使用ipython和提供的示例数据,我得到以下结果:

>>> run highlow
Maximum wind speed 61.0 on day 2
Minimum wind speed 2.3 on day 1
>>> data.speed.describe()
count    51.000000
mean     18.209804
std      16.784853
min       2.300000
25%       5.800000
50%      10.300000
75%      24.600000
max      61.000000
dtype: float64
>>> 

嘿,“最近的邮件”你是怎么让它们看起来像一个专栏的?非常感谢!他将文本格式化为代码。这可以通过将每行缩进四个空格来实现,或者通过选择一系列行并单击文本编辑器上方的
{}
按钮来实现。+1尽管Excel这个词不应该被格式化为
,因为它不是代码。
#! /usr/bin/env python      
import pandas as pd

# Export the tabs as csv-files: day1.csv, day2.csv, ..., day31.csv.
# Assume the first line is a header line and that columns are
# separated by ',':
#
# Height ,  Dir , Spd
# 139    ,  333 , 6.5
# 790    ,  343 , 5.9
# ...
#

# Use or own column names and skip header.
column_names = ['height', 'direction',  'speed']

# Read in the data for each day.
alldays = []
for d in range(1, 32):
    fname = "day{}.csv".format(d)
    frame = pd.read_csv(fname, names=column_names, header=0)
    frame['day'] = d
    alldays.append(frame)

# Concatenate all days into DataFrame.
data = pd.concat(alldays, ignore_index=True)

# Get index for max and use it to retrieve the day and the speed.
idx_max = data.speed.idxmax()
max_row = data.ix[idx_max]
print("Maximum wind speed {} on day {}".format(max_row.speed, int(max_row.day)))

# Same as above but for the minimum.
idx_min = data.speed.idxmin()
min_row = data.ix[idx_min]
print("Minimum wind speed {} on day {}".format(min_row.speed, int(min_row.day)))
>>> run highlow
Maximum wind speed 61.0 on day 2
Minimum wind speed 2.3 on day 1
>>> data.speed.describe()
count    51.000000
mean     18.209804
std      16.784853
min       2.300000
25%       5.800000
50%      10.300000
75%      24.600000
max      61.000000
dtype: float64
>>>