- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
鉴于以下内容,multiIndex
编辑了pd.DataFrame
:
Type p&l position rolldate value vola
Date Symbol
2008-01-02 AC 1757.2168 1 1201132800 45588.9161 480.6781
AUD 0.0000 0 1205280000 59872.0044 542.8067
BAX 551.1540 2 1208736000 165621.7706 125.8527
BTP 0.0000 0 -1 NaN 0.0000
C 674.4908 2 1202342400 14407.1226 137.4325
CAC40 0.0000 0 1200441600 55565.0000 580.2757
CAD 0.0000 0 1205280000 68784.0414 593.7115
CC 422.1133 1 1202428800 14276.9608 197.4064
CGB 482.2597 1 1203638400 79655.5288 299.6622
CHF -1216.9798 -1 1205280000 76431.4406 391.3853
CL 0.0000 0 1200355200 67824.0741 1268.3927
COIL 0.0000 0 1199750400 66612.2004 1088.8291
CT 296.1601 1 1202774400 23447.7124 239.7177
D 217.8649 1 1202688000 13201.2527 210.3416
DAX 0.0000 0 1205798400 200712.5000 1644.8412
DX 469.7712 -1 1205193600 51749.7277 215.9024
EMD 0.0000 0 1205366400 58135.8932 753.5315
ES 0.0000 0 1205366400 49649.3736 632.5416
ESTX50 -570.0000 1 1205798400 43780.0000 381.5206
EUR 0.0000 0 1205280000 125382.9657 605.9757
GBL -1020.0000 -1 1204588800 114130.0000 355.3088
GBM -730.0000 -1 1204588800 108670.0000 229.3634
GBP -93.6138 1 1205280000 84095.0095 477.9144
GBS -825.0000 -3 1204588800 103630.0000 100.1981
GBX 0.0000 0 1204588800 91280.0000 548.0709
GC 0.0000 0 1201219200 58551.1983 678.5194
GE 578.7037 2 1221523200 164760.3486 110.1067
GF 204.2484 -1 1203984000 36254.0850 261.8514
我试图通过日期
、符号
和列
名称访问行的值。到目前为止我只做到了这一点:
In [38]: df.ix['2008-01-02', 'AC']
Out[38]:
Type
Value benchmark NaN
cm 1.201824e+09
cm_next 1.204330e+09
margin NaN
nav NaN
p&l 1.757217e+03
position 1.000000e+00
rolldate 1.201133e+09
value 4.558892e+04
vola 4.806781e+02
Name: (2008-01-02, AC), dtype: float64
很接近我想要的;但是,我似乎无法弄清楚如何访问 Type
行。
df[df.loc['2008-01-02', 'AC']]['p&l'] # raises a KeyError
In [39]: df.ix['2008-01-02', 'AC']['Value'] # Gets me closer, but not quite there
Out[39]:
Type
benchmark NaN
cm 1.201824e+09
cm_next 1.204330e+09
margin NaN
nav NaN
p&l 1.757217e+03
position 1.000000e+00
rolldate 1.201133e+09
value 4.558892e+04
vola 4.806781e+02
Name: (2008-01-02, AC), dtype: float64
In [40]: df.ix['2008-01-02', 'AC']['Value']['p&l'] # raises another KeyError
我不能依赖 DataFrame.head()
或 .tail()
或任何其他类型的数字索引,因为我不能确定这些列将始终是顺序相同,每次运行的数量也不相等。
最佳答案
我认为你可以使用loc
- 参见docs - using slicers :
print df
p&l position rolldate value vola
Date Symbol
2008-01-02 AC 1757.2168 1 1201132800 45588.9161 480.6781
AUD 0.0000 0 1205280000 59872.0044 542.8067
BAX 551.1540 2 1208736000 165621.7706 125.8527
BTP 0.0000 0 -1 NaN 0.0000
C 674.4908 2 1202342400 14407.1226 137.4325
CAC40 0.0000 0 1200441600 55565.0000 580.2757
CAD 0.0000 0 1205280000 68784.0414 593.7115
CC 422.1133 1 1202428800 14276.9608 197.4064
CGB 482.2597 1 1203638400 79655.5288 299.6622
idx = pd.IndexSlice
print df.loc[idx['2008-01-02', 'AC'], idx['rolldate']]
1201132800.0
编辑:
如果 Multiindex
也在列中,请添加第一级 - Value
:
print df
Type Value
Date p&l position rolldate value vola
2008-01-02 AC 1757.2168 1 1201132800 45588.9161 480.6781
AUD 0.0000 0 1205280000 59872.0044 542.8067
BAX 551.1540 2 1208736000 165621.7706 125.8527
BTP 0.0000 0 -1 NaN 0.0000
C 674.4908 2 1202342400 14407.1226 137.4325
CAC40 0.0000 0 1200441600 55565.0000 580.2757
CAD 0.0000 0 1205280000 68784.0414 593.7115
CC 422.1133 1 1202428800 14276.9608 197.4064
CGB 482.2597 1 1203638400 79655.5288 299.6622
idx = pd.IndexSlice
print df.loc[idx['2008-01-02', 'AC'], idx['Value','rolldate']]
1201132800.0
关于python - 从 pd.df.ix[] View 中按行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36937051/
(请注意这里有一个问题 Pandas: group by and Pivot table difference ,但是这个问题是不同的。) 假设您从一个 DataFrame 开始 df = pd.Da
我在Windows 10上安装了Anaconda 3。我正在使用pd.read_csv()加载CSV文件,但收到错误消息。首先,我尝试了df=pd.read_csv(‘C:\Direct_market
我的输入数据是以下形式: gold,Program,MethodType,CallersT,CallersN,CallersU,CallersCallersT,CallersCallersN,
是否可以使用pd.merge复制以下内容 a = pd.DataFrame(dict(x=[1,2], y=[5,5])) b = pd.DataFrame(dict(x=[7,7], y=[12,1
我有一个像这样保存的数据框: Y_train_1.head() 0 4691.0 1 4661.0 2 4631.0 3 4601.0 4 4571.0 Y_train_
我有一个如下所示的 Excel 文件: CompanyName High Priority QualityIssue Customer1 Yes
题 为什么排序使用 pd.Series.sort_index使用分类索引时似乎不起作用?如何使用字母/数字以外的其他排序顺序对多索引 pd.Series 的索引进行排序? 移动电源 设置代码 impo
tt = pd.DataFrame({'a':[1,2,None,3],'b':[None,3,4,5]}) bb=pd.DataFrame(pd.isnull(tt).astype(int), in
示例代码: import pandas as pd import numpy as np sample = pd.DataFrame({"a":[1,2,3,1,2,3,1,2,3], "b":np.
我有一个 Pandas 系列和一个 Pandas 多索引数据框。 下面是一个简单的例子: iterables = [['milk', 'honey', 'dates'], ['jan', 'feb',
我拥有的: pd.Timestamp('2021-07-05 08:10:11') pd.Timestamp('2021-07-07 12:13:14') 我在找什么: [pd.Timestamp('
在使用 pandas 时,我遇到了创建新 data-Frame 的两种最常见的方法。使用pandas如下; 1. pandas.read_csv() Type: 2. pandas.DataFram
伙计们,我有一个Dataframe df= pd.DataFrame({'Point_ID':[1,2,3,1,2,1] , 'Shape_ID': [84,85,86,87,88,89],'LOL'
在 pandas datetimeindex 中,dayofweek和 weekday似乎是一样的。他们只是彼此的别名吗?我发现了这些功能 here 最佳答案 根据pandas源码定义的Datetim
我正在尝试按另一个按特定顺序排序的系列对 DataFrame (axis = 0) 进行排序。 例子:DataFrame 包含 CountryCodes 的索引:'AUS'、'BWA' ....(按字
我正在尝试使用 dask 读取 csv 文件,它给了我如下错误。但问题是我想要我的 ARTICLE_ID是 object(string) .谁能帮我成功读取数据? 回溯如下: ValueError:
为什么 pandas 有两个用于箱线图的函数:pandas.DataFrame.plot.box() 和 pandas.DataFrame.boxplot()? df = pd.DataFrame(n
我有一个多索引系列,如下所示。 > data = [['a', 'X', 'u', 1], ['a', 'X', 'v', 2], ['b', 'Y', 'u', 4], ['a', 'Z', 'u'
这个问题在这里已经有了答案: Inconsistency when setting figure size using pandas plot method (2 个答案) 关闭 4 年前。 在下面
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
我是一名优秀的程序员,十分优秀!