我的代码是
import numpy as np
import pandas as pd
ser_1 = pd.Series(np.random.randn(6))
ser_2 = pd.Series(np.random.randn(6))
ser_3 = pd.Series(np.random.randn(6))
df = pd.DataFrame(data= {'Col1': ser_1, 'Col2': ser_2, 'Col3':ser_3 } , )
df
它为我提供了一个由生成的rand #s组成的表:
Col1 Col2 Col3
0 -0.594436 -0.014419 0.512523
1 0.208414 0.804857 0.261830
2 1.714547 -0.765586 -0.153386
3 -0.834847 -0.683258 -1.341085
4 2.726621 0.379711 -0.276410
5 0.151987 0.622103 0.966635
但是,我想为行设置标签而不是0,1,... 5,我试过
df = pd.DataFrame(data= {'Col1': ser_1, 'Col2': ser_2, 'Col3':ser_3 } , index=['row0', 'row1', 'row2', 'row3', 'row4', 'row5', 'row6'] )
但正如预期的那样,它给了我NaNs
Col1 Col2 Col3
row0 NaN NaN NaN
row1 NaN NaN NaN
row2 NaN NaN NaN
row3 NaN NaN NaN
row4 NaN NaN NaN
row5 NaN NaN NaN
row6 NaN NaN NaN
问题是可以做什么,以便它不会给NaNs,我仍然可以标记它们?
答案 0 :(得分:1)
您可以直接设置索引:
In [11]: df.index = ['row0', 'row1', 'row2', 'row3', 'row4', 'row5']
In [12]: df
Out[12]:
Col1 Col2 Col3
row0 -1.094278 -0.689078 -0.465548
row1 1.555546 -0.388261 1.211150
row2 -0.143557 1.769561 -0.679080
row3 -0.064910 1.959216 0.227133
row4 -0.383729 0.113739 -0.954082
row5 0.434357 -0.646387 0.883319
注意:您也可以使用map(更清洁一点)来执行此操作:
df.index = df.index.map(lambda x: 'row%s' % x)
......虽然我应该说通常这不是你通常需要做的事情,保持整数指数是好事 TM 。
答案 1 :(得分:1)
列表理解也有效:
df.index = ['row{0}'.format(n) for n in range(df.index.shape[0])]
>>> df
Col1 Col2 Col3
row0 -1.213463 -1.331086 0.306792
row1 0.334060 -0.127397 -0.107466
row2 -0.893235 0.580098 -0.191778
row3 -0.663146 -1.269988 -1.303429
row4 0.418924 0.316321 -0.940015
row5 -0.082087 -1.893178 -1.809514
答案 2 :(得分:0)
为了能够在DataFrame构造函数上执行此操作,您需要使用nest dicts,并且索引用于从nest dict中提取值(这就是您获得NaN
的原因),例如:< / p>
>>> ser_1 = {'row{}'.format(i): v for i, v in enumerate(np.random.randn(6))}
>>> ser_2 = {'row{}'.format(i): v for i, v in enumerate(np.random.randn(6))}
>>> ser_3 = {'row{}'.format(i): v for i, v in enumerate(np.random.randn(6))}
>>> pd.DataFrame(data={'Col1': ser_1, 'Col2': ser_2, 'Col3':ser_3 },
... index=('row'+str(i) for i in range(6)))
Col1 Col2 Col3
row0 -0.431470 2.086320 -2.903402
row1 1.306443 1.431721 -0.344296
row2 -0.166202 -1.227531 0.351672
row3 0.929919 0.305378 0.233215
row4 0.553945 0.904051 0.681783
row5 1.424173 0.279041 -0.110876
但是根据@AndyHayden帖子创建后,你可以重新编制索引。