如何从数据框中提取数据并将其存储在变量中

时间:2019-03-27 10:40:16

标签: python-3.x pandas

我有一个数据框。我想从数据帧的特定列中提取数据,并将这些单独的数据块保存在不同的变量中。

这是我要从中提取数据的数据框。 我想在单独的变量中收集扬声器相同值的所有元素。

     segment  speaker start_time content
0   0.14    spk_0   0.14    From
1   0.47    spk_0   0.47    weekend
2   1.01    spk_0   1.01    All
3   1.18    spk_0   1.18    Star
4   1.35    spk_0   1.35    Game
5   1.56    spk_0   1.56    is
6   1.66    spk_0   1.66    over
7   1.66    spk_0   1.66    .
8   3.02    spk_0   3.02    It's
9   3.12    spk_0   3.12    time
10  3.29    spk_0   3.29    to
11  3.36    spk_0   3.36    get
12  3.49    spk_0   3.49    back
13  3.66    spk_0   3.66    down
14  3.84    spk_0   3.84    to
15  3.91    spk_0   3.91    business
16  3.91    spk_0   3.91    .
17  5.34    spk_0   5.34    You
18  5.41    spk_0   5.41    can't
19  5.61    spk_0   5.61    get
20  5.71    spk_0   5.71    that
21  5.84    spk_0   5.84    championship
22  5.84    spk_0   5.84    .
23  6.81    spk_0   6.81    It's
24  7.01    spk_0   7.01    fun
25  7.01    spk_0   7.01    .
26  7.22    spk_0   7.22    Weekend
27  8.66    spk_1   8.66    memorable
28  8.66    spk_1   8.66    .
29  9.64    spk_1   9.64    It
30  10.01   spk_1   10.01   couldn't
31  10.36   spk_1   10.36   be
32  10.74   spk_1   10.74   scripted
33  11.3    spk_1   11.3    any
34  11.69   spk_1   11.69   better
35  11.69   spk_1   11.69   .
36  13.02   spk_1   13.02   May
37  13.26   spk_1   13.26   be
38  13.39   spk_1   13.39   my
39  13.55   spk_1   13.55   last
40  13.91   spk_1   13.91   one
41  13.91   spk_1   13.91   .
42  14.1    spk_1   14.1    If
43  14.28   spk_1   14.28   this
44  15.25   spk_1   15.25   it'll
45  15.4    spk_1   15.4    be
46  15.52   spk_1   15.52   a
47  15.59   spk_1   15.59   hell
48  15.76   spk_1   15.76   of
49  15.84   spk_1   15.84   a
50  15.94   spk_1   15.94   DVD
51  15.94   spk_1   15.94   .

我想根据说话者的值映射内容中的所有值,并将其存储在同名变量中。

例如输出:

spk_0 - From weekend All Star Game is over . It's time to get back down to business . You can't get thatchampionship . It's fun . Weekend
spk_1 - memorable . It couldn't be scripted any better . May be my last one . If this it'll be a hell of a DVD .

1 个答案:

答案 0 :(得分:0)

尝试groupby()+ agg()

df.groupby('speaker').agg({'content' : ' '.join})