我遇到了一个奇怪的问题,即apply在数据帧上按行使用函数不会保留数据帧中值的数据类型。有没有办法在保留原始数据类型的数据帧上逐行应用函数?
下面的代码演示了这个问题。如果没有在下面int(...)的format函数中进行转换,就会出现错误,因为数据帧中的 int 在传入func.
import pandas as pd
df = pd.DataFrame({'int_col': [1, 2], 'float_col': [1.23, 4.56]})
print(df)
print(df.dtypes)
def func(int_and_float):
int_val, float_val = int_and_float
print('int_val type:', type(int_val))
print('float_val type:', type(float_val))
return 'int-{:03d}_float-{:5.3f}'.format(int(int_val), float_val)
df['string_col'] = df[['int_col', 'float_col']].apply(func, axis=1)
print(df)
Run Code Online (Sandbox Code Playgroud)
以下是运行上述代码的输出:
float_col int_col
0 1.23 1
1 4.56 2
float_col float64
int_col int64
dtype: object
int_val type: <class 'numpy.float64'>
float_val type: <class 'numpy.float64'>
int_val type: <class 'numpy.float64'>
float_val type: <class 'numpy.float64'>
float_col int_col …Run Code Online (Sandbox Code Playgroud) 我是Pelican的新手.我正在构建我的网站,以便我有两个类别:博客和项目.我有3个菜单按钮:主页,博客和项目.我正在尝试编辑我的base.html模板文件,以便Blog按钮处于活动状态,如果我在其中blog/或其任何子目录中,并且项目按钮处于活动状态,如果我在其中projects/或其任何子目录中.如果我有一个可访问的变量,base.html它给了我当前页面的相对URL,我可以将其拆分/并获取路径中的第一个目录.我一直在搜索,我似乎无法找到当前页面的相对URL的变量.是否有内置变量或者我可以为我正在寻找的定制变量?
我遵循了Docker + Django 教程,这很棒,因为我可以按照说明成功构建和运行网站。但是,我终生无法弄清楚如何在更改模型后成功运行数据库迁移。
以下是我采取的步骤:
设置一个名为的虚拟机 dev
docker-machine create -d virtualbox deveval $(docker-machine env dev)构建并启动它:
docker-compose builddocker-compose up -d运行初始迁移(这是我唯一一次能够运行看起来成功的迁移):
docker-compose run web python manage.py migrate通过导航到返回的 IP 地址来检查该网站是否正常工作:
docker-machine ip dev对模型进行更改。我刚刚将它添加到web/docker_django/apps/todo/models.py文件中的Item模型中。:
name = models.CharField(default='Unnamed', max_length=50, null=False)使用以下命令更新映像并重新启动容器:
docker-compose down --volumesdocker-compose builddocker-compose up --force-recreate -d我用了:
docker-compose run web python manage.py makemigrations todo
Run Code Online (Sandbox Code Playgroud)
然后:
docker-compose …Run Code Online (Sandbox Code Playgroud) 我可以在matplotlib中为行设置默认的颜色循环matplotlib.rcParams['axes.color_cycle'] = my_color_list,但我无法弄清楚如何用线条样式(即'-','--','-.',':')做同样的事情.我知道我可以使用类似的东西设置它
linecycler = itertools.cycle(lines)
for i in range(n):
plt.plot(x[i],y[i],next(linecycler))
Run Code Online (Sandbox Code Playgroud)
但是我希望能够做更像颜色循环的事情,所以每次我想绘制时都不需要制作新的循环仪.我怎样才能做到这一点?
.ttf如果在普通系统文件夹中找不到字体,我希望能够将文件放在本地文件夹中,并将 Matplotlib 配置为在该文件夹中查找字体。上一个答案显示了如何指向任何目录中的特定字体。这是答案中的代码:
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
path = '/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf'
prop = font_manager.FontProperties(fname=path)
mpl.rcParams['font.family'] = prop.get_name()
fig, ax = plt.subplots()
ax.set_title('Text in a cool font', size=40)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这样做的问题是,每次我想要 Helvetica(或在这种情况下 Comic Sans)在我的情节中时,我都必须这样做。我相信另一种解决方案是将 ttf 文件复制到类似~/anaconda/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf. . 我觉得应该有一些方法可以在我的~/.matplotlib/matplotlibrc文件中配置 matplotlib,这样如果我使用 Helvetica,我就不必每次都提供路径。如何将.ttf文件放置在自定义目录中(或至少一个对 python 或 matplotlib 更新是安全的)并且每次绘图时都不必重新键入文件路径?
奖励积分,如果解决方案允许我到一个相对路径使用返回的目录import matplotlib; matplotlib.get_configdir(),因为我的一些机器,是~/.config/matplotlib和一些人来说~/.matplotlib。
假设我有一个与 x、y 和 z 值对应的一维值数组,如下所示:
x y z arr_1D
0 0 0 0
1 0 0 1
0 1 0 2
1 1 0 3
0 2 0 4
1 2 0 5
0 0 1 6
...
0 2 3 22
1 2 3 23
Run Code Online (Sandbox Code Playgroud)
我想arr_1D进入一个arr_3D具有形状的 3D 数组(nx,ny,nz)(在这种情况下(2,3,4))。我希望使用 可以引用这些值arr_3D[x_index, y_index, z_index],例如,arr_3D[1,2,0]=5. Usingnumpy.reshape(arr_1D, (2,3,4))给了我一个正确尺寸的 3D 矩阵,但没有按照我想要的方式排序。我知道我可以使用以下代码,但我想知道是否有办法避免笨重的嵌套 for 循环。
arr_1d = np.arange(24)
nx = 2
ny = …Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的目录结构:
$ tree
.
|-- dir
| `-- subdir
| `-- data
`-- makefile
Run Code Online (Sandbox Code Playgroud)
哪里data是文件.我makefile看起来像这样:
all: dir/analysis
.SECONDEXPANSION:
%/analysis: %/subdir
%/analysis: $(addsuffix /data, $$^)
@echo TARGET: $@ DEPS: $^
touch $@
Run Code Online (Sandbox Code Playgroud)
当我跑步时make,我希望结果看起来像这样:
TARGET: dir/analysis DEPS: dir/subdir/data dir/subdir
touch dir/analysis
Run Code Online (Sandbox Code Playgroud)
相反,它只是报道
make: *** No rule to make target `dir/analysis', needed by `all'. Stop.
Run Code Online (Sandbox Code Playgroud)
如果我将第一个规则更改为dir/analysis: dir/subdir然后它按预期工作.因此我怀疑make忽略了第一条规则,并在第一条规则出现时直接跳到第二条规则%/analysis: %/subdir.当两个规则都dir/analysis作为目标而不仅仅是第一个规则时,它也可以按预期工作.MadScientists对这里不同问题的回答似乎适用于我的问题.我尝试添加类似的规则
dummy_target: dir/subdir
mkdir -p dir/subdir
Run Code Online (Sandbox Code Playgroud)
和
dir/subdir:
mkdir -p …Run Code Online (Sandbox Code Playgroud)