我正在寻找类似网格网格函数的清晰比较.不幸的是我找不到它!
Numpy http://docs.scipy.org/doc/numpy/reference/提供
mgrid
ogrid
meshgrid
Scitools http://hplgit.github.io/scitools/doc/api/html/index.html提供
ndgrid
boxgrid
理想情况下,总结所有这些的表格将是完美的!
我想在Mac OS X 10.6.8上安装Python Pandas库(0.8.1).这个库需要Numpy> = 1.6.
我试过这个
$ sudo easy_install pandas
Searching for pandas
Reading http://pypi.python.org/simple/pandas/
Reading http://pandas.pydata.org
Reading http://pandas.sourceforge.net
Best match: pandas 0.8.1
Downloading http://pypi.python.org/packages/source/p/pandas/pandas-0.8.1.zip#md5=d2c5c5bea971cd760b0ae6f6850fcb74
Processing pandas-0.8.1.zip
Running pandas-0.8.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ckAMym/pandas-0.8.1/egg-dist-tmp-0mlL7t
error: Setup script exited with pandas requires NumPy >= 1.6 due to datetime64 dependency
Run Code Online (Sandbox Code Playgroud)
所以我试着安装Numpy
$ sudo easy_install numpy
Searching for numpy
Best match: numpy 1.6.2
Adding numpy 1.6.2 to easy-install.pth file
Using /Library/Python/2.6/site-packages
Processing dependencies for numpy
Finished processing dependencies for numpy
Run Code Online (Sandbox Code Playgroud)
所以我又试了一次
$ …Run Code Online (Sandbox Code Playgroud) 我有一个二进制文件,其中包含一个平面位置的记录.每条记录看起来像:
0x00: Time, float32
0x04: X, float32 // X axis position
0x08: Y, float32 // Y axis position
0x0C: Elevation, float32
0x10: float32*4 = Quaternion (x,y,z axis and w scalar)
0x20: Distance, float32 (unused)
Run Code Online (Sandbox Code Playgroud)
所以每条记录长32个字节.
我想得到一个Numpy阵列.
在偏移1859处,存在无符号的int 32(4字节),其指示阵列的元素的数量.在我的情况下12019.
我不关心(现在)标题数据(偏移1859之前)
数组仅从偏移1863(= 1859 + 4)开始.
我定义了自己的Numpy dtype之类的
dtype = np.dtype([
("time", np.float32),
("PosX", np.float32),
("PosY", np.float32),
("Alt", np.float32),
("Qx", np.float32),
("Qy", np.float32),
("Qz", np.float32),
("Qw", np.float32),
("dist", np.float32),
])
Run Code Online (Sandbox Code Playgroud)
我正在阅读文件使用fromfile:
a_bytes = np.fromfile(filename, dtype=dtype)
Run Code Online (Sandbox Code Playgroud)
但我没有看到任何参数提供fromfile传递偏移量.
我想将JSON数据存储在Python Pandas DataFrame中
我的JSON数据是像这样的dicts的词典
d = {
"col1": {
"row1": {
"data1": "0.87",
"data2": "Title col1",
"data3": "14.4878",
"data4": "Title row1"
},
"row2": {
"data1": "15352.3",
"data2": "Title col1",
"data3": "14.9561",
"data4": "Title row2"
},
"row3": {
"data1": "0",
"data2": "Title col1",
"data3": "16.8293",
"data4": "Title row3"
}
},
"col2": {
"row1": {
"data1": "0.87",
"data2": "Title col2",
"data3": "24.4878",
"data4": "Title row1"
},
"row2": {
"data1": "15352.3",
"data2": "Title col2",
"data3": "24.9561",
"data4": "Title row2"
},
"row3": {
"data1": …Run Code Online (Sandbox Code Playgroud) 我想在给定日期添加一个月
import datetime
dt = datetime.datetime(year=2014, month=5, day=2)
Run Code Online (Sandbox Code Playgroud)
所以我应该得到
datetime.datetime(year=2014, month=6, day=2)
Run Code Online (Sandbox Code Playgroud)
但随着
dt = datetime.datetime(year=2015, month=1, day=31)
Run Code Online (Sandbox Code Playgroud)
我应该得到
datetime.datetime(year=2015, month=3, day=1)
Run Code Online (Sandbox Code Playgroud)
因为没有2015-02-31(而且我希望我的结果在一天之后回合)
有些月份有31天,有些月份有30天,有些是29天,有些是28天!
所以添加一个datetime.timedelta可能不是一个好的做法(因为我们不知道要添加的天数)
我注意到熊猫有一个有趣的概念 DateOffset
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#dateoffset-objects
但我没有找到Month偏移,只是MonthBegin或MonthEnd
我也看到这篇文章 如何使用datetime Python模块计算当前日期六个月的日期?
所以我试过dateutil.relativedelta但是
from dateutil.relativedelta import relativedelta
datetime.datetime(year=2015, month=1, day=31)+relativedelta(months=1)
Run Code Online (Sandbox Code Playgroud)
回报
datetime.datetime(2015, 2, 28, 0, 0)
Run Code Online (Sandbox Code Playgroud)
所以结果在前一天得到了结果.
是否有(干净)的方式来圆天之后?
编辑:我给了一个例子,一个月要添加,但我也希望能够添加例如:2年零6个月(使用一个relativedelta(years=2, months=6))
我有一个名为csv文件data.csv,如
TS;val
10:00;0.1
10:05;0.2
10:10;0.3
10:15;0.4
Run Code Online (Sandbox Code Playgroud)
我使用这个脚本读了这个csv文件
#!/usr/bin/env python
import pandas as pd
if __name__ == "__main__":
yyyy = 2013
mm = 2
dd = 1
df = pd.read_csv('data.csv', sep=';', parse_dates=[0], index_col=0)
print(df)
Run Code Online (Sandbox Code Playgroud)
我明白了
val
TS
2013-06-17 10:00:00 0.1
2013-06-17 10:05:00 0.2
2013-06-17 10:10:00 0.3
2013-06-17 10:15:00 0.4
Run Code Online (Sandbox Code Playgroud)
我想将每个DateTimeIndex的日期更改为2013-02-01
val
TS
2013-02-01 10:00:00 0.1
2013-02-01 10:05:00 0.2
2013-02-01 10:10:00 0.3
2013-02-01 10:15:00 0.4
Run Code Online (Sandbox Code Playgroud)
更简单的方法是什么?
pandas.Series对象确实有很多to_*功能,但缺少一个to_excel功能.是否有更简单/更好的方法来完成此代码段第3行的导出?仅仅为简单的I/O将系列转换为DataFrame感觉很笨:
import numpy as np
import pandas as pd
s = pd.Series([1,3,5,np.nan,6,8])
pd.DataFrame(s).to_excel('s.xlsx', 's')
Run Code Online (Sandbox Code Playgroud) 以下代码
import numpy as np
import itertools
a_p1 = np.arange(0, 4, 1)
a_p2 = np.arange(20, 25, 1)
params = itertools.product(a_p1, a_p2)
for (p1, p2) in params:
print(p1, p2)
Run Code Online (Sandbox Code Playgroud)
输出
(0, 20) (0, 21) (0, 22) (0, 23) (0, 24) (1, 20) (1, 21) (1, 22) (1, 23) (1, 24) (2, 20) (2, 21) (2, 22) (2, 23) (2, 24) (3, 20) (3, 21) (3, 22) (3, 23) (3, 24)
Run Code Online (Sandbox Code Playgroud)
2嵌套for循环也可以输出相同的结果
for i, p1 in enumerate(a_p1):
for j, p2 in enumerate(a_p2): …Run Code Online (Sandbox Code Playgroud) 我有一个df像这样的数据帧:
t pos
frame
0 2015-11-21 14:46:32.843517000 0.000000
1 NaT 0.000000
2 NaT 0.000000
3 NaT 0.000000
4 NaT 0.000000
5 NaT 0.000000
6 NaT 0.000000
7 NaT 0.000000
8 NaT 0.000000
9 NaT 0.000000
10 NaT 0.000000
11 NaT 0.000000
12 NaT 0.000000
13 NaT 0.000000
14 NaT 0.000000
15 NaT 0.000000
16 NaT 0.000000
17 NaT 0.000000
18 NaT 0.000000
19 NaT 0.000000
... ... ...
304 2015-11-21 14:46:54.255383750 12.951807
305 2015-11-21 14:46:54.312271250 5.421687
306 2015-11-21 …Run Code Online (Sandbox Code Playgroud) 我想用 Python Pandas 读取一个如下所示的 Excel 文件:
https://www.dropbox.com/s/1usfr3fxfy2qlpp/header_with_merged_cells.xlsx?dl=0
我们可以看到这个 Excel 文件有一个合并单元格的标题
我做了
import pandas as pd
df = pd.read_excel("header_with_merged_cells.xlsx", skiprows=3)
print(df)
print(df.dtypes)
print(df.columns)
Run Code Online (Sandbox Code Playgroud)
它返回一个数据帧,如:
ColA ColB ColC Unnamed: 3 Unnamed: 4 ColD
0 NaT NaN 1 2.0 3 NaN
1 2010-01-01 A A 2.1 2010-02-01 00:00:00 E
2 2010-01-02 B C 2.2 2010-02-02 00:00:00 F
Run Code Online (Sandbox Code Playgroud)
dtypes 喜欢:
ColA datetime64[ns]
ColB object
ColC object
Unnamed: 3 float64
Unnamed: 4 object
ColD object
Run Code Online (Sandbox Code Playgroud)
columns 喜欢:
Index(['ColA', 'ColB', 'ColC', 'Unnamed: 3', 'Unnamed: 4', 'ColD'], …Run Code Online (Sandbox Code Playgroud)