region year val
1.0 2015.0 6.775457e+05
1.0 2016.0 6.819761e+05
1.0 2017.0 6.864065e+05
2.0 2015.0 6.175457e+05
2.0 2016.0 6.419761e+05
3.0 2017.0 6.564065e+05
Run Code Online (Sandbox Code Playgroud)
在上面的数据框中,我想计算连续行之间的百分比差异,但仅针对相同的区域值.我试过这个,但不确定它是否有效.什么是实现它的最佳方法?
df.groupby(['region', 'year'])['val'].pct_change()
Run Code Online (Sandbox Code Playgroud) 我正在尝试根据日期将数据框分成两个。这已经解决了这里的一个相关问题:Split dataframe into two based on date
我的数据框如下所示:
abcde col_b
2008-04-10 0.041913 0.227050
2008-04-11 0.041372 0.228116
2008-04-12 0.040835 0.229199
2008-04-13 0.040300 0.230301
2008-04-14 0.039770 0.231421
Run Code Online (Sandbox Code Playgroud)
我如何根据日期拆分它(比如 2008-04-12 之前和之后)?当我尝试这个时:
df.loc[pd.to_datetime(df.index) <= split_date]
Run Code Online (Sandbox Code Playgroud)
这里split_date是datetime.date(2008-04-12),我得到这个错误:
*** TypeError: <class 'datetime.date'> type object 2008-04-12
Run Code Online (Sandbox Code Playgroud) 我想用None替换xarray数据集中变量中的值。我尝试了这种方法,但是没有用:
da[da['var'] == -9999.]['var'] = None
Run Code Online (Sandbox Code Playgroud)
我收到此错误: *** TypeError: unhashable type: 'numpy.ndarray'
我可以在这里使用类似numpy replace的东西吗?da是xarray数据集。这里是什么da样子:
<xarray.Dataset>
Dimensions: (band: 1, time: 3, x: 4258, y: 2334)
Coordinates:
* band (band) int32 1
* y (y) float64 4.406e+06 4.406e+06 4.406e+06 4.406e+06 4.406e+06 ...
* x (x) float64 1.125e+05 1.126e+05 1.127e+05 1.128e+05 1.129e+05 ...
* time (time) datetime64[ns] 2005-12-31 2006-12-31 2007-12-31
Data variables:
var (time, band, y, x) float32 dask.array<shape=(3, 1, 2334, 4258), chunksize=(1, 1, 2334, 4258)>
Run Code Online (Sandbox Code Playgroud)
这是da.var的样子:
<xarray.DataArray 'var' …Run Code Online (Sandbox Code Playgroud) 如何将年份和月份名称转换为此数据帧的datetime列:
region year Months
0 alabama 2018 January
1 alabama 2018 February
2 alabama 2018 March
3 alabama 2018 April
4 alabama 2018 May
Run Code Online (Sandbox Code Playgroud)
当我这样做:
pd.to_datetime(df_sub['year'] * 10000 + df_sub['Months'] * 100, format='%Y%m')
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
*** TypeError: unsupported operand type(s) for +: 'int' and 'str'
Run Code Online (Sandbox Code Playgroud) 在下面的示例中,
pipe = Pipeline([
('scale', StandardScaler()),
('reduce_dims', PCA(n_components=4)),
('clf', SVC(kernel = 'linear', C = 1))])
param_grid = dict(reduce_dims__n_components=[4,6,8],
clf__C=np.logspace(-4, 1, 6),
clf__kernel=['rbf','linear'])
grid = GridSearchCV(pipe, param_grid=param_grid, cv=3, n_jobs=1, verbose=2)
grid.fit(X_train, y_train)
print(grid.score(X_test, y_test))
Run Code Online (Sandbox Code Playgroud)
我正在使用StandardScaler(),这也是将其应用于测试集的正确方法吗?
我有以下数据框:
id Lat Lon Year Area State
50319 -36.0629 -62.3423 2019 90 Iowa
18873 -36.0629 -62.3423 2017 90 Iowa
18876 -36.0754 -62.327 2017 124 Illinois
18878 -36.0688 -62.3353 2017 138 Kansas
Run Code Online (Sandbox Code Playgroud)
我想创建一个新的列,其指定基于列是否唯一的标识符Lat,Lon并且Area具有相同的价值观。例如,在这种情况下,行1和2具有在那些列中的相同的值,并且将被给予相同的唯一标识符0_Iowa,其中Iowa来自于State列中。我尝试使用 for 循环,但有没有更 Pythonic 的方法来做到这一点?
id Lat Lon Year Area State unique_id
50319 -36.0629 -62.3423 2019 90 Iowa 0_Iowa
18873 -36.0629 -62.3423 2017 90 Iowa 0_Iowa
18876 -36.0754 -62.327 2017 124 Illinois 1_Illinois
18878 -36.0688 -62.3353 2017 …Run Code Online (Sandbox Code Playgroud) 在下图中,我使用 OpenCV harris 角点检测器仅检测正方形的角点(以及外部正方形内的较小正方形)。但是,我还检测到图像侧面数字的角点。我怎样才能让它只关注正方形而不是数字?我需要一种在执行 OpenCV 角点检测时忽略数字的方法。代码、输入图像和输出图像如下:
import cv2 as cv
img = cv.imread(filename)
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv.cornerHarris(gray, 2, 3, 0.04)
dst = cv.dilate(dst,None)
# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]
cv.imshow('dst', img)
Run Code Online (Sandbox Code Playgroud)
输入图像
Harris 角点检测器的输出
我想动态分配2D数组的1维(给出另一个维度).这有用吗:
int NCOLS = 20;
// nrows = user input...
double *arr[NCOLS];
arr = (double *)malloc(sizeof(double)*nrows);
Run Code Online (Sandbox Code Playgroud)
并释放它:
free(arr)
Run Code Online (Sandbox Code Playgroud) 我有一个包含约 50,000 行和 300 列的 csv 文件。执行以下操作会导致 Pandas (python) 中出现内存错误:
merged_df.stack(0).reset_index(1)
Run Code Online (Sandbox Code Playgroud)
数据框如下所示:
GRID_WISE_MW1 Col0 Col1 Col2 .... Col300
7228260 1444 1819 2042
7228261 1444 1819 2042
Run Code Online (Sandbox Code Playgroud)
我正在使用最新的 pandas (0.13.1),并且行数较少 (~2,000) 的数据帧不会出现该错误
谢谢!
我有pandas数据框(df)形式的以下数据:
每个列名称都有以"_"分隔的起始和结束节点.即,'A_B'表示A的起始节点和B的结束节点
A_A A_B A_C B_B B_A B_C C_C C_A C_B
12 23 33 43 19 98 44 77 35
Run Code Online (Sandbox Code Playgroud)
我想将它转换为一个2D矩阵,输出到csv,使它看起来像:
A B C
A 12 23 33
B 19 43 98
C 77 35 44
Run Code Online (Sandbox Code Playgroud)
要做到这一点,假设我们有
from itertools import groupby, combinations_with_replacement
col_names = ['A','B','C']
for i in combinations_with_replacement(col_names, 2):
get_val = df[i[0]+'_'+i[1]]
Run Code Online (Sandbox Code Playgroud)
在pandas中有一种方法可以将其输出为矩阵并打印出行和列名称吗?