6

我正在从数据框创建一个系列,其中 df 的一列是索引,另一列是该系列的数据。

这是我的代码:

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...   

有没有人可以帮助我解决这个问题?我已经为此工作了一周,但我没有想法!

4

1 回答 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
于 2013-07-03T16:39:08.110 回答