簡體   English   中英

具有字符串列到整數列表列的數據框

[英]Dataframe with column of strings to column of integer lists

我有一個數據框,其中在一列中,每一行的數據都是這樣的字符串:

[[25570], [26000]]

我希望該系列中的每個條目都成為整數列表。

IE瀏覽器:

[25570,26000] ^ ^ int int

到目前為止,我可以將其保存到字符串列表中,但保留空白:

s = s.str.replace("[","").str.replace("]","")
    s = s.str.replace(" ","").str.split(",")

數據框字典:

     f =  {'chunk': {0: '[72]',
  1: '[72, 68]',
  2: '[72, 68, 65]',
  3: '[72, 68, 65, 70]',
  4: '[72, 68, 65, 70, 67]',
  5: '[72, 68, 65, 70, 67, 74]',
  6: '[68]',
  7: '[68, 65]',
  8: '[68, 65, 70]',
  9: '[68, 65, 70, 67]'},
 'chunk_completed': {0: '[25570]',
  1: '[26000]',
  2: '[26240]',
  3: '[26530]',
  4: '[26880]',
  5: '[27150]',
  6: '[26000]',
  7: '[26240]',
  8: '[26530]',
  9: '[26880]'},
 'chunk_id': {0: '72',
  1: '72-68',
  2: '72-68-65',
  3: '72-68-65-70',
  4: '72-68-65-70-67',
  5: '72-68-65-70-67-74',
  6: '68',
  7: '68-65',
  8: '68-65-70',
  9: '68-65-70-67'},
 'diffs_avg': {0: nan,
  1: 430.0,
  2: 335.0,
  3: 320.0,
  4: 327.5,
  5: 316.0,
  6: nan,
  7: 240.0,
  8: 265.0,
  9: 293.3333333333333},
 'sd': {0: nan,
  1: nan,
  2: 134.35028842544406,
  3: 98.48857801796105,
  4: 81.80260794538685,
  5: 75.3657747256671,
  6: nan,
  7: nan,
  8: 35.355339059327385,
  9: 55.075705472861024},
 'timecodes': {0: '[[25570]]',
  1: '[[25570], [26000]]',
  2: '[[25570], [26000], [26240]]',
  3: '[[25570], [26000], [26240], [26530]]',
  4: '[[25570], [26000], [26240], [26530], [26880]]',
  5: '[[25570], [26000], [26240], [26530], [26880], [27150]]',
  6: '[[26000]]',
  7: '[[26000], [26240]]',
  8: '[[26000], [26240], [26530]]',
  9: '[[26000], [26240], [26530], [26880]]'}}

嘗試這個

f = pd.DataFrame().from_dict(s, orient='index')
f.columns = ['timecodes']
f['timecodes'].apply(lambda x: [a[0] for a in eval(x) if a])

輸出量

Out[622]:
0                                        [25570]
1                                 [25570, 26000]
2                          [25570, 26000, 26240]
3                   [25570, 26000, 26240, 26530]
4            [25570, 26000, 26240, 26530, 26880]
5     [25570, 26000, 26240, 26530, 26880, 27150]
6                                        [26000]
7                                 [26000, 26240]
8                          [26000, 26240, 26530]
9                   [26000, 26240, 26530, 26880]
10           [26000, 26240, 26530, 26880, 27150]
11                                       [26240]
12                                [26240, 26530]
13                         [26240, 26530, 26880]
14                  [26240, 26530, 26880, 27150]
15                                       [26530]
16                                [26530, 26880]
17                         [26530, 26880, 27150]
18                                       [26880]
19                                [26880, 27150]
Name: 0, dtype: object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM