这是我的数据帧
import pandas as pd
df = pd.DataFrame({'A': ['one', 'one', 'two', 'two', 'one'] ,
'B': ['Ar', 'Br', 'Cr', 'Ar','Ar'] ,
'C': ['12/15/2011', '11/11/2001', '08/30/2015', '07/3/1999','03/03/2000' ],
'D':[1,7,3,4,5]})
Run Code Online (Sandbox Code Playgroud)
我的目标是按列A
进行分组,并按列对分组结果进行排序B
.
这是我想出的:
sort_group = df.sort_values('B').groupby('A')
Run Code Online (Sandbox Code Playgroud)
我希望分组操作不会扭曲顺序,但它不起作用,也不返回数据帧,而是返回groupby
对象
<pandas.core.groupby.DataFrameGroupBy object at 0x0000000008B190B8>
Run Code Online (Sandbox Code Playgroud)
有什么建议?
这是我的数据框:
categ <- c('a','a','a','b','b')
value <- c(1,2,5,4,5)
df <- data.frame(categ, value)
Run Code Online (Sandbox Code Playgroud)
我想按categ
列分组并删除每组中的第一个/最后一个元素。理想情况下,我想创建一个列,指示每个组中的第一个/最后一个元素。这是我尝试过的,但没有奏效:
library('plyr')
ddply(df, .(categ), function(x) x[-nrow(x)])
Run Code Online (Sandbox Code Playgroud)
PS 这可能是重复的问题,但我是 R 的新手,R 非常神秘,所以我无法立即移植解决方案。
我正在尝试将以下格式转换为日期:
as.Date('Mar.17', format = '%b.%y')
Run Code Online (Sandbox Code Playgroud)
但它返回 NA。
我缺少什么?
更新,我期待 2017 年 3 月,而不是 2018 年
如何在numpy中创建反对角矩阵?我当然可以手动完成,但是很好奇是否有功能。
我正在寻找一个矩阵,矩阵从左下角到右上角,其他地方为零。
我正在尝试在 Google Colaboratory 上使用带有 GPU 的 XGBoost。这是我的笔记本:
import numpy as np
import os
import xgboost as xgb
train_X = np.random.rand(100,5)
train_Y = np.random.choice(2, 100)
test_X = np.random.rand(10,5)
test_Y = np.random.choice(2, 10)
xg_train = xgb.DMatrix(train_X, label=train_Y)
xg_test = xgb.DMatrix(test_X, label=test_Y)
param = {}
# use softmax multi-class classification
param['objective'] = 'multi:softmax'
# scale weight of positive examples
param['eta'] = 0.1
param['max_depth'] = 6
param['silent'] = 1
param['nthread'] = 4
param['num_class'] = 2
param['gpu_id'] = 0
param['max_bin'] = 16
param['tree_method'] = 'gpu_hist' …
Run Code Online (Sandbox Code Playgroud) 我想从 sklearn 的 Tfidfvectorizer 对象中获取矩阵。这是我的代码:
from sklearn.feature_extraction.text import TfidfVectorizer
text = ["The quick brown fox jumped over the lazy dog.",
"The dog.",
"The fox"]
vectorizer = TfidfVectorizer()
vectorizer.fit_transform(text)
Run Code Online (Sandbox Code Playgroud)
这是我尝试并返回错误的方法:
vectorizer.toarray()
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-117-76146e626284> in <module>() ----> 1 vectorizer.toarray() AttributeError: 'TfidfVectorizer' object has no attribute 'toarray'
另一种尝试
vectorizer.todense()
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-118-6386ee121184> in <module>() ----> 1 vectorizer.todense() AttributeError: 'TfidfVectorizer' object has no attribute 'todense'
以下代码不想编译.我收到一条错误消息:
'System.Collections.Generic.List.Add(string [])的最佳重载方法匹配有一些无效的参数
但我看不出有什么问题.有什么建议?
public static List<string[]> ReadFromDelimitedFile (string Path)
{
int i=0;
List<string[]> AllContracts = new List<string[]>();
using (TextFieldParser parser = new TextFieldParser(Path))
{
parser.Delimiters = new string[] { "\t" };
while (true)
{
AllContracts.Add(i)= parser.ReadFields();
if (AllContracts.Add(i) == null)
{
break;
}
i++;
}
}
return AllContracts;
}
Run Code Online (Sandbox Code Playgroud) 我试图理解map_partitions
in 的dask
作用。这是我的例子:
import dask.dataframe as dd
import pandas as pd
from dask.multiprocessing import get
import random
df = pd.DataFrame({'col_1':random.sample(range(10000), 100), 'col_2': random.sample(range(10000), 100) })
def test_f(df):
print(df.col_1)
print("------------")
ddf = dd.from_pandas(df, npartitions=8)
ddf['result'] = ddf.map_partitions(test_f ).compute(get=get)
Run Code Online (Sandbox Code Playgroud)
这是输出:
0 1.0
1 1.0
Name: col_1, dtype: float64
------------
Run Code Online (Sandbox Code Playgroud)
为什么我无法完整打印我的数据框?输出是什么意思?
这是我的例子.
my_df <- data.frame(col_1 = c(1,2),
col_2 = c(as.Date('2018-11-11'), as.Date('2016-01-01')))
dates_list <- my_df$col_2
for(el in dates_list){
print(el)
}
Run Code Online (Sandbox Code Playgroud)
它产生:
17846
16801
Run Code Online (Sandbox Code Playgroud)
如何输出日期呢?我可以使用显式索引来完成它,但希望有更简单的解决方案
我跑了
renv::status()
Run Code Online (Sandbox Code Playgroud)
回来了:
Error in seq_len(length(splat) - 1) :
argument must be coercible to non-negative integer
Run Code Online (Sandbox Code Playgroud)
我尝试traceback()
并回来了:
f(n[[i]], x[[i]], ...)
7: FUN(X[[i]], ...)
6: lapply(idx, callback)
5: enumerate(fields, function(section, entries) {
splat <- strsplit(section, "/", fixed = TRUE)[[1]]
for (i in seq_len(length(splat) - 1)) {
k <- splat[1:i]
if (is.null(data[[k]]))
data[[k]] <<- list()
}
data[[splat]] <<- entries
})
4: renv_lockfile_read(lockpath)
3: renv_status_check_missing_lockfile(project, lockpath)
2: renv_status_impl(project, libpaths, lockpath, cache)
1: renv::status()
Run Code Online (Sandbox Code Playgroud)
我从哪里开始排除故障?
PS我使用的是Windows 10和R 4.0.4
更新 这里是 renv.lock 文件 …