请考虑以下简单示例
group <-c('A','A','A','B','B','B','B')
names<- c(NA,'fred',NA,'josh','josh',NA,NA)
data=data_frame(group,names)
> data
# A tibble: 7 × 2
  group names
  <chr> <chr>
1     A  <NA>
2     A  fred
3     A  <NA>
4     B  josh
5     B  josh
6     B  <NA>
7     B  <NA>
在这里,我想获得,每个group第一个非缺失的名称names.我怎样才能做到这一点?下面的解决方案使用coalesce和first失败.
data %>% group_by(group) %>% mutate(first_non_missing = first(names),
                                    first_non_missing_alt = coalesce(names)) %>% ungroup()
# A tibble: 7 × 4
  group names first_non_missing first_non_missing_alt
  <chr> <chr>             <chr>                 <chr>
1     A  <NA>              <NA>                  <NA>
2 …我有一个pandas数据帧,我正在使用有用的.plot()方法.
数据框看起来像
col1   col2   col3
1      2      3
7      0      3
1      2      2
因此df.plot(),我用它来获得我想要的图表.
问题是,我想将图表导出为pdf.理想情况下,我还可以生成其他图表(基于此数据框)并将其添加到pdf中.
有可能这样做吗?谢谢!
我有一个非常大的pandas数据集,在某些时候我需要使用以下函数
def proc_trader(data):
    data['_seq'] = np.nan
    # make every ending of a roundtrip with its index
    data.ix[data.cumq == 0,'tag'] = np.arange(1, (data.cumq == 0).sum() + 1)
    # backfill the roundtrip index until previous roundtrip;
    # then fill the rest with 0s (roundtrip incomplete for most recent trades)
    data['_seq'] =data['tag'].fillna(method = 'bfill').fillna(0)
    return data['_seq']
    # btw, why on earth this function returns a dataframe instead of the series `data['_seq']`??
我用申请
reshaped['_spell']=reshaped.groupby(['trader','stock'])[['cumq']].apply(proc_trader)
显然,我不能在这里分享数据,但你看到我的代码中存在瓶颈吗?可能是那arange件事吗?数据中有许多name-productid组合.
最小工作范例:
import pandas as pd …考虑以下示例:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                         'foo', 'bar', 'foo', 'foo'],
                   'B' : [12,10,-2,-4,-2,5,8,7],
                   'C' : [-5,5,-20,0,1,5,4,-4]})
df
Out[12]: 
     A   B   C
0  foo  12  -5
1  bar  10   5
2  foo  -2 -20
3  bar  -4   0
4  foo  -2   1
5  bar   5   5
6  foo   8   4
7  foo   7  -4
在这里,我需要计算,对于A中的每个组,B 条件中C中元素的总和是非负的(即> = 0,基于另一列的条件).反之亦然C.
但是,我的代码失败了.
df.groupby('A').agg({'B': lambda x: x[x.C>0].sum(),
                     'C': lambda …我datetime在我的pig脚本中使用的一些Python udfs中使用.到现在为止还挺好.我在Cloudera 5.5上使用了猪12.0
但是,我也需要使用pytz或者dateutil包,它们似乎不是一个vanilla python安装的一部分.
我可以Pig在某些方面在我的udfs中使用它们吗?如果是这样,怎么样?我认为dateutil是安装在我的节点上(我不是管理员,所以我怎么能实际检查是这种情况?),但是当我输入时:
import sys
#I append the path to dateutil on my local windows machine. Is that correct?
sys.path.append('C:/Users/me/AppData/Local/Continuum/Anaconda2/lib/site-packages')
from dateutil import tz
在我的udfs.py剧本中,我得到:
2016-08-30 09:56:06,572 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1121: Python Error. Traceback (most recent call last):
  File "udfs.py", line 23, in <module>
    from dateutil import tz
ImportError: No module named dateutil
当我运行我的猪脚本.
我所有的其他python udfs(datetime例如使用)工作得很好.知道怎么解决这个问题吗?
非常感谢!
UPDATE
在用python路径玩了一下后,我现在能够 …
我正在为以下示例而苦苦挣扎
time = c('2013-01-03 21:59:21.549', '2013-01-04 22:00:21.549', '2013-01-05 22:01:21.222', '2013-01-06 22:06:23.559' )
value = c(1,2,3,4)
data <- data_frame(time, value)
data <-data %>%  mutate(time = ymd_hms(time))
> data
# A tibble: 4 × 2
                 time value
               <dttm> <dbl>
1 2013-01-03 21:59:21     1
2 2013-01-04 22:00:21     2
3 2013-01-05 22:01:21     3
4 2013-01-06 22:06:23     4
我怎样才能写出一份dplyr::filter声明,而不是每天只保留21:59和22:01(包括)之间的观察结果?
与hour(time)和分开玩minute(time)在这里似乎不太好。
我在这里错过了什么吗?
预期输出:仅第 1,2 和 3 行。谢谢!
mylist = []
for this in that:
  df = 1
  mylist.append(df)
基本上只是创建一个空列表,然后将在循环中创建的对象添加到其中。
我只看到R解决方案,其中必须指定新元素的索引(例如mylist[[i]] <- df),因此需要i在循环中创建索引。
有没有比仅在最后一个元素之后追加的方法更简单的方法了。
我正在努力解决以下问题
我已经下载了PLUTO NYC Manhattan Shapefile为纽约市的税收很多从这里https://www1.nyc.gov/site/planning/data-maps/open-data/dwn-pluto-mappluto.page
我能够sf用简单的方式阅读它们st_read
> mydf
Simple feature collection with 42638 features and 90 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 971045.3 ymin: 188447.4 xmax: 1010027 ymax: 259571.5
epsg (SRID):    NA
proj4string:    +proj=lcc +lat_1=40.66666666666666 +lat_2=41.03333333333333 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs
First 10 features:
   Borough Block  Lot  CD CT2010 CB2010 SchoolDist Council ZipCode FireComp PolicePrct HealthCent HealthArea
1       MN  1545   52 108    138   4000         02       5   10028     E022 …请考虑以下示例
dtrain <- data_frame(text = c("Chinese Beijing Chinese",
                              "Chinese Chinese Shanghai",
                              "Chinese Macao",
                              "Tokyo Japan Chinese"),
                     doc_id = 1:4,
                     class = c(1, 1, 1, 0))
dtrain_spark <- copy_to(sc, dtrain, overwrite = TRUE)
> dtrain_spark
# Source:   table<dtrain> [?? x 3]
# Database: spark_connection
  text                     doc_id class
  <chr>                     <int> <dbl>
1 Chinese Beijing Chinese       1     1
2 Chinese Chinese Shanghai      2     1
3 Chinese Macao                 3     1
4 Tokyo Japan Chinese           4     0
在这里,我有经典的Naive Bayes示例,class用于识别属于该China类别的文档.
我可以sparklyr …
考虑这个例子
mydata <- data_frame(ind_1 = c(NA,NA,3,4),
                     ind_2 = c(2,3,4,5),
                     ind_3 = c(5,6,NA,NA),
                     y = c(28,34,25,12),
                     group = c('a','a','b','b'))
> mydata
# A tibble: 4 x 5
  ind_1 ind_2 ind_3     y group
  <dbl> <dbl> <dbl> <dbl> <chr>
1    NA     2     5    28 a    
2    NA     3     6    34 a    
3     3     4    NA    25 b    
4     4     5    NA    12 b 
在这里,我希望,对于每个group,回归y该组中没有丢失的任何变量,并将相应的lm对象存储在一个list-column.
那是:
a,这些变量对应于ind_2和ind_3b,他们对应ind_1 …r ×6
python ×4
dplyr ×3
pandas ×3
apache-pig ×1
apache-spark ×1
append ×1
cloudera ×1
datetime ×1
jython ×1
list ×1
lm ×1
lubridate ×1
matplotlib ×1
performance ×1
purrr ×1
pytz ×1
r-sf ×1
sparklyr ×1