Sta*_*cey 7 python python-3.x pandas
我有一个名为staticData的数据框,看起来像这样:
narrow_sector broad_sector country exchange \
unique_id
BBG.MTAA.STM.S Semiconductors Technology CH MTAA
BBG.MTAA.CNHI.S Machinery-Diversified Industrial GB MTAA
BBG.MTAA.FCA.S Auto Manufacturers Consumer Cyclical GB MTAA
BBG.MTAA.A2A.S Electric Utilities IT MTAA
BBG.MTAA.ACE.S Electric Utilities IT MTAA
Run Code Online (Sandbox Code Playgroud)
我正试图逐行遍历数据帧,以选择索引(unique_id)和交换的两位信息。我在遍历索引时遇到问题。请查看我的代码:
for i, row in staticData.iterrows():
unique_id = staticData.ix[i]
exchange = row['exchange']
Run Code Online (Sandbox Code Playgroud)
我已经尝试过unique_id = row ['unique_id'],但无法使其正常工作...
我正在尝试返回第1行的发言
unique_id = BBG.MTAA.STM.S
exchange = MTAA
Run Code Online (Sandbox Code Playgroud)
您需要以下内容:
for i, row in staticData.iterrows():
unique_id = i
exchange = row['exchange']
Run Code Online (Sandbox Code Playgroud)
我将成为索引标签的值
例:
In [57]:
df = pd.DataFrame(np.random.randn(5,3), index=list('abcde'), columns=list('fgh'))
df
Out[57]:
f g h
a -0.900835 -0.913989 -0.624536
b -0.854091 0.286364 -0.869539
c 1.090133 -0.771667 1.258372
d -0.721753 -0.329211 0.479295
e 0.520786 0.273722 0.824172
In [62]:
for i, row in df.iterrows():
print('index: ', i, 'col g:', row['g'])
index: a col g: -0.913988608754
index: b col g: 0.286363847188
index: c col g: -0.771666520074
index: d col g: -0.329211394286
index: e col g: 0.273721527592
Run Code Online (Sandbox Code Playgroud)
可能是更熊猫的方式?
staticData.apply((lambda x: (x.name, x['exchange'])), axis=1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27216 次 |
| 最近记录: |