我的代码是
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
它会给我一个包含生成的随机数的表格:
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'] )
但是如预期所述,它会返回NaN(非数字)
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
问题是如何避免出现 NaN,并仍然能够对它们进行标记?