我想减少下面箱子图中框的宽度.这是我的代码,但它不起作用:
bp = plt.boxplot(boxes, widths = 0.6, patch_artist = True)
Run Code Online (Sandbox Code Playgroud)
我想将数据框中的所有数字列转换为它们的绝对值,并且正在执行以下操作:
df = df.abs()
Run Code Online (Sandbox Code Playgroud)
但是,它给出了错误:
*** TypeError: bad operand type for abs(): 'unicode'
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?我真的不想手动指定列名
df_masked.loc[:, col] = df_masked.groupby([df_masked.index.month, df_masked.index.day])[col].\
transform(lambda y: y.fillna(y.median()))
Run Code Online (Sandbox Code Playgroud)
即使在使用.loc之后,我也会得到这个.错误,我该如何解决?
Anaconda\lib\site-packages\pandas\core\indexing.py:476: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s
Run Code Online (Sandbox Code Playgroud) 我有以下数据框:
date Values
3/1/2018
3/3/2018 0
3/5/2018 -0.011630952
3/8/2018 0.024635792
3/10/2018
3/10/2018 0.013662755
3/13/2018 2.563770771
3/15/2018 0.026081264
3/17/2018
3/25/2018 4.890818119
3/26/2018
3/28/2018 0.994944572
3/30/2018 0.098569691
4/2/2018
4/2/2018 2.261398315
4/4/2018 2.595984459
4/7/2018 2.145072699
4/9/2018 2.401818037
4/11/2018
4/12/2018 2.233839989
4/14/2018 2.179880142
4/17/2018 0.173141539
4/18/2018
4/19/2018 0.04037559
4/22/2018 2.813424349
4/24/2018 2.764060259
4/27/2018
5/2/2018 4.12789917
5/4/2018 4.282546997
5/4/2018
5/7/2018 5.083333015
5/13/2018
5/14/2018 1.615991831
5/17/2018 0.250209153
5/19/2018 5.003758907
5/20/2018
5/22/2018
5/24/2018 0.177665412
5/29/2018
6/1/2018 3.190019131
6/3/2018 3.514900446
6/5/2018 2.796386003
6/6/2018 4.132686615
6/8/2018
6/11/2018 2.82530117
6/14/2018 …Run Code Online (Sandbox Code Playgroud) 我已经编写了一些代码来使用pandas中的数据透视表来计算加权平均值.但是,我不确定如何添加执行加权平均的实际列(添加新列,其中每行包含'cumulative'/'COUNT'的值).
数据看起来像这样:
VALUE COUNT GRID agb
1 43 1476 1051
2 212 1476 2983
5 7 1477 890
4 1361 1477 2310
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
# Read input data
lup_df = pandas.DataFrame.from_csv(o_dir+LUP+'.csv',index_col=False)
# Insert a new column with area * variable
lup_df['cumulative'] = lup_df['COUNT']*lup_df['agb']
# Create and output pivot table
lup_pvt = pandas.pivot_table(lup_df, 'agb', rows=['GRID'])
# TODO: Add a new column where each row contains value of 'cumulative'/'COUNT'
lup_pvt.to_csv(o_dir+PIVOT+'.csv',index=True,header=True,sep=',')
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
是否有netcdf操作符(来自nco或任何python netcdf库)可用于覆盖netcdf文件中的特定单元格?
我想更改包含全球气候数据的netcdf文件中的小区域的值.谢谢!
我想从以下数据框创建一个堆积条形图:
VALUE COUNT RECL_LCC RECL_PI
0 1 15686114 3 1
1 2 27537963 1 1
2 3 23448904 1 2
3 4 1213184 1 3
4 5 14185448 3 2
5 6 13064600 3 3
6 7 27043180 2 2
7 8 11732405 2 1
8 9 14773871 2 3
Run Code Online (Sandbox Code Playgroud)
情节中会有2个酒吧.一个用于RECL_LCC,另一个用于RECL_PI.每个条形中有3个部分对应于RECL_LCC和RECL_PI中的唯一值,即1,2,3,并将每个部分的COUNT相加.到目前为止,我有这样的事情:
df = df.convert_objects(convert_numeric=True)
sub_df = df.groupby(['RECL_LCC','RECL_PI'])['COUNT'].sum().unstack()
sub_df.plot(kind='bar',stacked=True)
Run Code Online (Sandbox Code Playgroud)
但是,我得到这个情节:

有关如何解决它的任何想法?我对groupby做错了,但不确定解决方案
def function_name(df):
for i, row in df.iterrows():
df.set_value(...)
return df
if __name__ == '__main__':
# Assume we have a dataframe called idf
idf = function_name(idf)
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我将一个名为 idf 的数据帧传递到一个名为 function_name 的函数中。在该函数中,我循环遍历数据帧中的所有行,进行一些修改并返回一个数据帧,并将其存储回 idf 中。
我有一种感觉,这种方法正在浪费内存,有人可以纠正我或指出更好的Pythonic方法吗?请注意,我有充分的理由使用 iterrows,即使它使一切变慢,我只是想要一些关于我将数据帧传递给函数并将其返回的方式的反馈
- -编辑 -
根据@marius 的反馈,我想知道的是:
通过将数据帧传递到函数中,我是否制作了数据帧的新副本?这就是我担心的内存浪费
@click.group(context_settings=dict(help_option_names=['-h', '--help']))
def plot_glm():
pass
@plot_glm.command()
@click.argument('path_nc')
@click.argument('out_path')
@click.argument('var_name')
@click.option('--xaxis_min', default=0.0, help='')
@click.option('--xaxis_max', default=1.1, help='')
@click.option('--xaxis_step', default=0.1, help='')
@click.option('--annotate_date', help='')
@click.option('--yr', default=0, help='')
@click.option('--date', default=-1, help='')
@click.option('--xlabel', default='', help='')
@click.option('--title', default='', help='')
@click.option('--tme_name', default='time', help='')
@click.option('--show_plot', help='')
@click.option('--any_time_data', help='')
@click.option('--format', default='%.2f', help='')
@click.option('--land_bg', help='')
@click.option('--cmap', default=plt.cm.RdBu, help='')
@click.option('--grid', help='')
@click.option('--fill_mask', help='')
def plot_map_from_nc(path_nc, out_path, var_name, xaxis_min=0.0, xaxis_max=1.1, xaxis_step=0.1,
annotate_date=False, yr=0, date=-1, xlabel='', title='', tme_name='time', show_plot=False,
any_time_data=True, format='%.2f', land_bg=True, cmap=plt.cm.RdBu, grid=False, fill_mask=False)
if __name__ == '__main__':
plot_glm()
Run Code Online (Sandbox Code Playgroud)
使用python click库时,出现此错误(python版本2.7.11,Windows 10,单击版本6.6):
ctx …Run Code Online (Sandbox Code Playgroud) 有没有办法获得XGRegressor预测的置信区间?像scikit随机森林的forest-ci包?我想要一个python的解决方案
python ×10
pandas ×6
boxplot ×1
cdo-climate ×1
matplotlib ×1
nco ×1
netcdf ×1
numpy ×1
pivot-table ×1
plot ×1
python-click ×1
scipy ×1
stacked ×1
xgboost ×1