我想将列表加载到pandas DataFrame的列中,但似乎无法简单地执行此操作.这是我想要使用的一个例子,transpose()但我认为这是不必要的:
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: x = np.linspace(0,np.pi,10)
In [4]: y = np.sin(x)
In [5]: data = pd.DataFrame(data=[x,y]).transpose()
In [6]: data.columns = ['x', 'sin(x)']
In [7]: data
Out[7]:
x sin(x)
0 0.000000 0.000000e+00
1 0.349066 3.420201e-01
2 0.698132 6.427876e-01
3 1.047198 8.660254e-01
4 1.396263 9.848078e-01
5 1.745329 9.848078e-01
6 2.094395 8.660254e-01
7 2.443461 6.427876e-01
8 2.792527 3.420201e-01
9 3.141593 1.224647e-16
[10 rows x 2 columns]
Run Code Online (Sandbox Code Playgroud)
有没有办法直接将每个列表加载到列中以消除转置并在创建DataFrame时插入列标签?
我试图计算大约十万点之间的所有距离.我有以下代码用Fortran编写并使用以下代码编译f2py:
C 1 2 3 4 5 6 7
C123456789012345678901234567890123456789012345678901234567890123456789012
subroutine distances(coor,dist,n)
double precision coor(n,3),dist(n,n)
integer n
double precision x1,y1,z1,x2,y2,z2,diff2
cf2py intent(in) :: coor,dist
cf2py intent(in,out):: dist
cf2py intent(hide)::n
cf2py intent(hide)::x1,y1,z1,x2,y2,z2,diff2
do 200,i=1,n-1
x1=coor(i,1)
y1=coor(i,2)
z1=coor(i,3)
do 100,j=i+1,n
x2=coor(j,1)
y2=coor(j,2)
z2=coor(j,3)
diff2=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2)
dist(i,j)=sqrt(diff2)
100 continue
200 continue
end
Run Code Online (Sandbox Code Playgroud)
我正在使用以下python代码编译fortran代码setup_collision.py:
# System imports
from distutils.core import *
from distutils import sysconfig
# Third-party modules
import numpy
from numpy.distutils.core import Extension, setup
# Obtain the numpy include directory. …Run Code Online (Sandbox Code Playgroud) 我有几个加权值,我正在加权平均值.我想使用加权值和加权平均值来计算加权标准差.如何修改典型标准偏差以包括每次测量的权重?
这是我正在使用的标准差公式.

当我只使用' x '的每个加权值和'\ bar { x }' 的加权平均值时,结果似乎小于它应该的值.
在演示 lines的Datashader示例笔记本之后,输入是 Pandas DataFrame(尽管 Dask DataFrame 似乎也可以工作)。我的数据在一个 NumPy 数组中。我可以使用 Datashader 绘制来自 NumPy 数组的线条,而无需先将它们放入 DataFrame 中吗?
line glyph的文档似乎表明这是可能的,但我没有找到一个例子。我链接到的示例笔记本使用Canvas.line了我在文档中没有找到的。
我想将一个 pandas.DataFrame 存储到一个文本文件中,该文件的列使用空格字符对齐。如果这是我的示例 DataFrame:
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: df = pd.DataFrame(np.linspace(0,1,9).reshape(3,3))
In [4]: df
Out[4]:
0 1 2
0 0.000 0.125 0.250
1 0.375 0.500 0.625
2 0.750 0.875 1.000
[3 rows x 3 columns]
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
In [5]: df.to_csv('test.txt', sep='?')
Run Code Online (Sandbox Code Playgroud)
得到这个:
In [6]: more test.txt
0 1 2
0 0.0 0.125 0.25
1 0.375 0.5 0.625
2 0.75 0.875 1.0
Run Code Online (Sandbox Code Playgroud)
我应该使用什么分隔符?我想知道是否有办法在不使用\t角色的情况下做到这一点。看起来很好
0 1 2
0 0.0 0.125 0.25 …Run Code Online (Sandbox Code Playgroud) 我读过这篇关于自动格式化代码的文章,它在格式化一些 JSON 时起作用。似乎一次性格式化了整个文件。这可能有点激烈,例如,如果 VS Code 使用的缩进规则与项目冲突,则在大文件上使用时将导致主要版本控制差异。如何自动缩进当前行?
在 Emacs 中,光标可以位于行上的任何位置,按Ctrl+i将自动缩进当前行。我搜索了可能的键盘快捷键,但没有找到可以执行此操作的键盘快捷键。我正在寻找类似“缩进线”的东西,但要自动缩进。
我有一个大小为 n_slice x 2048 x 3 的 numpy 坐标数组,其中 n_slice 数以万计。我想分别对每个 2048 x 3 切片应用以下操作
import numpy as np
from scipy.spatial.distance import pdist
# load coor from a binary xyz file, dcd format
n_slice, n_coor, _ = coor.shape
r = np.arange(n_coor)
dist = np.zeros([n_slice, n_coor, n_coor])
# this loop is what I want to parallelize, each slice is completely independent
for i in xrange(n_slice):
dist[i, r[:, None] < r] = pdist(coor[i])
Run Code Online (Sandbox Code Playgroud)
我尝试通过制作coor一个来使用 Dask dask.array,
import dask.array as …Run Code Online (Sandbox Code Playgroud) 当我用诗歌添加tensoflow(诗歌添加张量流)时,我收到此错误:
Using version ^2.7.0 for tensorflow
Updating dependencies
Resolving dependencies... (0.8s)
SolverProblemError
The current project's Python requirement (>=3.6,<4.0) is not compatible with some of the required packages Python requirement:
- tensorflow-io-gcs-filesystem requires Python >=3.6, <3.10, so it will not be satisfied for Python >=3.10,<4.0
- tensorflow-io-gcs-filesystem requires Python >=3.6, <3.10, so it will not be satisfied for Python >=3.10,<4.0
- tensorflow-io-gcs-filesystem requires Python >=3.6, <3.10, so it will not be satisfied for Python >=3.10,<4.0
....
For tensorflow-io-gcs-filesystem, a possible solution …Run Code Online (Sandbox Code Playgroud) 我确信这已被问到但我找不到,所以我对冗余道歉.
我想使用grep或egrep来查找其中包含"P"或"CA"的每一行,并将它们传递给新文件.我可以使用以下方法轻松完成:
egrep ' CA ' all.pdb > CA.pdb
Run Code Online (Sandbox Code Playgroud)
要么
egrep ' P ' all.pdb > P.pdb
Run Code Online (Sandbox Code Playgroud)
我是regex的新手,所以我不确定它的语法or.
更新: 输出行的顺序很重要,即我不希望输出对匹配的字符串排序.以下是一个文件的前8行示例:
ATOM 1 N THR U 27 -68.535 88.128 -17.857 1.00 0.00 1H5 N
ATOM 2 HT1 THR U 27 -69.437 88.216 -17.434 0.00 0.00 1H5 H
ATOM 3 HT2 THR U 27 -68.270 87.165 -17.902 0.00 0.00 1H5 H
ATOM 4 HT3 THR U 27 -68.551 88.520 -18.777 0.00 0.00 1H5 H
ATOM 5 CA LYS B 122 …Run Code Online (Sandbox Code Playgroud) python ×7
numpy ×2
pandas ×2
arrays ×1
auto-indent ×1
dask ×1
dataframe ×1
datashader ×1
f2py ×1
fortran ×1
grep ×1
list ×1
matplotlib ×1
regex ×1
statistics ×1
tensorflow ×1