当传入索引时,Pandas - series创建结果为NaN?

8
我将从一个数据框创建一个系列,其中数据框的一列是索引,另一列是系列的数据。
以下是我的代码:
miniframe = attendframe[:20]
s = pd.Series(miniframe.yes, index = miniframe.event)
s[:10]

然而,如果我包含 index = miniframe.event 部分,我会得到一个空系列,如下所示:
1159822043    NaN
686467261     NaN
1186208412    NaN
2621578336    NaN
855842686     NaN
2018671985    NaN
488116622     NaN
1273761447    NaN
2688888297    NaN
3870329460    NaN

原始数据框看起来像这样:

         event                                                yes  \
0   1159822043  1975964455 252302513 4226086795 3805886383 142...   
1    686467261  2394228942 2686116898 1056558062 3792942231 41...   
2   1186208412                                                NaN   
3   2621578336                                                NaN   
4    855842686  2406118796 3550897984 294255260 1125817077 109...   
5   2018671985                                                NaN   
6    488116622  4145960786 2550625355 2577667841 1575121941 28...   
7   1273761447  2680366192 2151335654 3447231284 3021641283 17...   
8   2688888297  298428624 2292079981 1819927116 1843127538 410...   
9   3870329460                                                NaN   
10  3041357942  4238605842 769099880 4281206081 1756250815 187...   

有没有人能帮我解决这个问题?我已经在做了一个星期了,但是现在已经没什么想法了!


尝试这个:attendframe.set_index('event').ix[:20,'yes'] - Jeff
1个回答

1
In [84]: df = DataFrame(dict(event = randint(10,100,(100)), yes = ['foo','bar']*50))

In [85]: df.loc[[2,3,5,10,15],'yes'] = np.nan

In [86]: df.head(10)
Out[86]: 
   event  yes
0     47  foo
1     94  bar
2     71  NaN
3     62  NaN
4     43  foo
5     60  NaN
6     90  foo
7     43  bar
8     15  foo
9     16  bar

In [87]: mini = df[:20]

In [88]: mini
Out[88]: 
    event  yes
0      47  foo
1      94  bar
2      71  NaN
3      62  NaN
4      43  foo
5      60  NaN
6      90  foo
7      43  bar
8      15  foo
9      16  bar
10     26  NaN
11     64  bar
12     82  foo
13     63  bar
14     16  foo
15     78  NaN
16     49  foo
17     32  bar
18     34  foo
19     46  bar

In [89]: Series(mini.yes.values,mini.event).iloc[:10]
Out[89]: 
event
47       foo
94       bar
71       NaN
62       NaN
43       foo
60       NaN
90       foo
43       bar
15       foo
16       bar
dtype: object

请注意,这是其中一个 .ix 无法正常工作的情况;使用 .iloc 并明确说明(忽略我之前的评论!)
In [92]: df.set_index('event').iloc[:10].loc[:,'yes']
Out[92]: 
event
47       foo
94       bar
71       NaN
62       NaN
43       foo
60       NaN
90       foo
43       bar
15       foo
16       bar
Name: yes, dtype: object

非常棒的答案。我可以问一个澄清问题吗?你代码的真正关键是mini.yes.values - 为什么我需要访问列的值才能将它们解析成系列对象? - analystic
1
values会返回底层的ndarray;它允许您将值与新索引(事件)映射。将系列传递给Series构造函数和一个新索引将使用传递的索引重新索引,而不是在这种情况下想要的。 - Jeff
现在这就很清楚了!再次感谢! - analystic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接