我有一个像我这样的子集的数据框:
a b x y
0 1 2 3 -1
1 2 4 6 -2
2 3 6 6 -3
3 4 8 3 -4
df = df[(df.a >= 2) & (df.b <= 8)]
df = df.groupby(df.x).mean()
Run Code Online (Sandbox Code Playgroud)
如何使用pandas管道运算符表达这一点?
df = (df
.pipe((x.a > 2) & (x.b < 6)
.groupby(df.x)
.apply(lambda x: x.mean())
Run Code Online (Sandbox Code Playgroud) 我有这个人.熊猫数据帧:
df.shape
(86, 245)
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时:
df[0, :]
Run Code Online (Sandbox Code Playgroud)
我收到错误:
*** TypeError: unhashable type
Run Code Online (Sandbox Code Playgroud)
我该如何解决?我只想得到第一排
如何使用datetime作为索引创建pandas数据框,以及列的随机值.目前,我有这个:
from datetime import datetime, timedelta
date_today = datetime.now()
date_end = date_today + timedelta(7)
df = pd.DataFrame(columns=['test'])
Run Code Online (Sandbox Code Playgroud)
我该如何从这里开始?
我在Python中有以下数据框(实际的数据框要大得多,只是提供一个小样本):
A B C D E F
0 0.43 0.52 0.96 1.17 1.17 2.85
1 0.43 0.52 1.17 2.72 2.75 2.94
2 0.43 0.53 1.48 2.85 2.83
3 0.47 0.59 1.58 3.14
4 0.49 0.80
Run Code Online (Sandbox Code Playgroud)
我使用df.values将数据帧转换为numpy,然后将其传递给boxplot.
当我尝试从这个pandas数据帧中创建一个boxplot时,从每列中选取的值的数量被限制为列中的最小值(在本例中为F列).有什么方法可以从每一列中包装出所有值吗?
注意:我使用df.dropna删除每列中缺少值的行.但是,这会将数据帧的大小调整为列长度的最小公分母,并且会使绘图变得混乱.
import prettyplotlib as ppl
import numpy as np
import pandas
import matplotlib as mpl
from matplotlib import pyplot
df = pandas.DataFrame.from_csv(csv_data,index_col=False)
df = df.dropna()
labels = ['A', 'B', 'C', 'D', 'E', 'F']
fig, ax = pyplot.subplots()
ppl.boxplot(ax, df.values, xticklabels=labels)
pyplot.show()
Run Code Online (Sandbox Code Playgroud) 我想为以下pandas数据帧绘制一个boxplot:
> p1.head(10)
N0_YLDF MAT
0 1.29 13.67
1 2.32 10.67
2 6.24 11.29
3 5.34 21.29
4 6.35 41.67
5 5.35 91.67
6 9.32 21.52
7 6.32 31.52
8 3.33 13.52
9 4.56 44.52
Run Code Online (Sandbox Code Playgroud)
我想箱形图是'N0_YLDF'列,但它们应该用'MAT'分层.当我使用foll时.命令:
p1.boxplot(column='N0_YLDF',by='MAT')
Run Code Online (Sandbox Code Playgroud)
它使用所有唯一的MAT值,在完整的p1数据帧数约为15,000.这导致难以理解的箱线图.
有没有什么方法可以对MAT值进行分层,这样我就可以获得不同的第二个MAT值的N0_YLDF箱图,依此类推....
谢谢!
我得到了foll.尝试使用joblib缓存结果时的用户警告:
from tempfile import mkdtemp
cachedir = mkdtemp()
from joblib import Memory
memory = Memory(cachedir=cachedir, verbose=0)
@memory.cache
def get_nc_var3d(path_nc, var, year):
"""
Get value from netcdf for variable var for year
:param path_nc:
:param var:
:param year:
:return:
"""
try:
hndl_nc = open_or_die(path_nc)
val = hndl_nc.variables[var][int(year), :, :]
except:
val = numpy.nan
logger.info('Error in getting var ' + var + ' for year ' + str(year) + ' from netcdf ')
hndl_nc.close()
return val
Run Code Online (Sandbox Code Playgroud)
我得到了foll.使用foll调用此函数时发出警告.参数:
UserWarning: Persisting input arguments took …Run Code Online (Sandbox Code Playgroud) from mlxtend.regressor import StackingRegressor
from sklearn.ensemble.forest import RandomForestRegressor as RFR
from sklearn.ensemble import GradientBoostingRegressor as GBR
import xgboost as xgb
rfr = RFR(n_estimators=500, n_jobs=cc.ncpu, random_state=0)
gbr = GBR(n_estimators=1000, random_state=0)
xgr = xgb.XGBRegressor()
mtr = RFR() # meta regressor
regressors = [rfr, gbr, xgr]
model = StackingRegressor(regressors=regressors, meta_regressor=mtr)
param_grid = {
'fs__threshold': ['median'],
'fs__estimator__max_features': ['log2'],
'clf__rfr__max_features': ['auto', 'log2'],
'clf__gbr__learning_rate': [0.05, 0.02, 0.01],
'clf__gbr__max_depth': [4, 5, 6, 7],
'clf__gbr__max_features': ['auto', 'log2'],
'clf__gbr__n_estimators': [500, 1000, 2000],
'clf__xgr__learning_rate': [0.001, 0.05, 0.1, 0.2],
'clf__xgr__max_depth': [2, …Run Code Online (Sandbox Code Playgroud) 是否有任何关于 matplotlib 中哪个后端最快的指导?在 Windows 和 Mac 上。
我试过:
import matplotlib
matplotlib.use('PS')
Run Code Online (Sandbox Code Playgroud) 我想用以下方法迭代拟合曲线到python中的数据:
我可以拟合多项式曲线,如下所示:
vals = array([0.00441025, 0.0049001 , 0.01041189, 0.47368389, 0.34841961,
0.3487533 , 0.35067096, 0.31142986, 0.3268407 , 0.38099566,
0.3933048 , 0.3479948 , 0.02359819, 0.36329588, 0.42535543,
0.01308297, 0.53873956, 0.6511364 , 0.61865282, 0.64750302,
0.6630047 , 0.66744816, 0.71759617, 0.05965622, 0.71335208,
0.71992683, 0.61635697, 0.12985441, 0.73410642, 0.77318621,
0.75675988, 0.03003641, 0.77527201, 0.78673995, 0.05049178,
0.55139476, 0.02665514, 0.61664748, 0.81121749, 0.05521697,
0.63404375, 0.32649395, 0.36828268, 0.68981099, 0.02874863,
0.61574739])
x_values = np.linspace(0, 1, len(vals))
poly_degree = 3
coeffs = np.polyfit(x_values, vals, poly_degree)
poly_eqn = np.poly1d(coeffs)
y_hat = poly_eqn(x_values)
Run Code Online (Sandbox Code Playgroud)
如何执行步骤2和3?
import filecmp
comparison = filecmp.dircmp(dir_local, dir_server)
comparison.report_full_closure()
Run Code Online (Sandbox Code Playgroud)
我想将本地计算机上保存的所有 CSV 文件与服务器上保存的文件进行比较。两者的文件夹结构相同。我只想进行数据比较而不是元数据(如创建时间等)。我正在使用filecmp但它似乎执行元数据比较。有没有办法做我想做的事?