Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字符串列到整数列表列的Dataframe_Python_Pandas_Dataframe - Fatal编程技术网

Python 从字符串列到整数列表列的Dataframe

Python 从字符串列到整数列表列的Dataframe,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个dataframe,其中在一列中,每行的数据是如下所示的字符串: [[25570], [26000]] 我希望序列中的每个条目都成为一个整数列表 即: [2557026000] ^ ^ 整型 到目前为止,我可以将其添加到字符串列表中,但保留空格: s = s.str.replace("[","").str.replace("]","") s = s.str.replace(" ","").str.split(",") 数据帧的Dict: f = {'c

我有一个dataframe,其中在一列中,每行的数据是如下所示的字符串:

[[25570], [26000]]
我希望序列中的每个条目都成为一个整数列表

即:

[2557026000] ^ ^ 整型

到目前为止,我可以将其添加到字符串列表中,但保留空格:

s = s.str.replace("[","").str.replace("]","")
    s = s.str.replace(" ","").str.split(",")
数据帧的Dict:

     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

在原始字符串中没有值的情况下,如何使其适用?ie[[]]我不理解这个解决方案,无法让它工作。这是一个更大的df的一部分,仅供参考。我假设您在目录中有另一个类似[[]的条目。对。。如果是这样的话,更新应该会起作用,我在列表中添加了一个复选框“if a”。。这应该在数据帧中起作用,是的。如果我的数据帧是“f”,而问题列是“时间码”,我应该如何格式化您的答案?