将具有字符串列,数组列的表转换为所有字符串列

时间:2019-03-23 19:46:36

标签: python pandas

我正在尝试将包含字符串列和数组列的表转换为仅包含字符串列的表

Here is how current table looks like:

+-----+--------------------+--------------------+
|col1 |         col2       |         col3       |
+-----+--------------------+--------------------+
|   1 |[2,3]               | [4,5]              |
|   2 |[6,7,8]             | [8,9,10]           |
+-----+--------------------+--------------------+




How can I get expected result like that:

+-----+--------------------+--------------------+
|col1 |         col2       |         col3       |
+-----+--------------------+--------------------+
|   1 | 2                  | 4                  |
|   1 | 3                  | 5                  |
|   2 | 6                  | 8                  |
|   2 | 7                  | 9                  |
|   2 | 8                  | 10                 |
+-----+--------------------+--------------------+

2 个答案:

答案 0 :(得分:0)

将列转换为列表,然后转换为laravel/installer,最后将它们转换为numpy.array

DataFrame

答案 1 :(得分:0)

混淆来自混合标量列和列表列。

在假设-col2和col3的长度相同的情况下,我们可以先将所有标量列转换为列表列,然后进行串联:

df = pd.DataFrame({'col1': [1,2], 
                   'col2': [[2,3] , [6,7,8]], 
                   'col3': [[4,5], [8,9,10]]})

# First, we turn all columns into list columns
df['col1'] = df['col1'].apply(lambda x: [x]) * df['col2'].apply(len)

# Then we concatenate the lists
df.apply(np.concatenate)

输出:

  col1  col2 col3
0   1   2   4
1   1   3   5
2   2   6   8
3   2   7   9
4   2   8   10