Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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 如何在保持目标列不变的情况下转储数据帧中的字符串列表?_Python_Pandas_Dataframe_Text Processing - Fatal编程技术网

Python 如何在保持目标列不变的情况下转储数据帧中的字符串列表?

Python 如何在保持目标列不变的情况下转储数据帧中的字符串列表?,python,pandas,dataframe,text-processing,Python,Pandas,Dataframe,Text Processing,我有一个dataframe,其中第一列文本包含字符串列表,其他两列Label1和Label2包含标签。数据框如下所示: |Text |Label1 | Label2 | ----------------------------------------- | ['text1', 'text2', | 0 | 0 'text3'] ----------------------------------------- | ['text4', '

我有一个dataframe,其中第一列文本包含字符串列表,其他两列Label1和Label2包含标签。数据框如下所示:

|Text                |Label1  |  Label2 |
----------------------------------------- 
| ['text1', 'text2', |  0     |    0 
   'text3']
-----------------------------------------
| ['text4', 'text5', |  1     |    1 
   'text6']
-----------------------------------------
    ....         ..           .. 
|  Text              |Label1  |  Label2 |
----------------------------------------- 
|  text1             |  0     |    0    |
-----------------------------------------
|  text2             |  0     |    0    |
-----------------------------------------
|  text3             |  0     |    0    |
-----------------------------------------
|  text4             |  1     |    1    |
-----------------------------------------
|  text5             |  1     |    1    |
-----------------------------------------
|  text6             |  1     |    1    |
-----------------------------------------
    ....         ..           .. 
现在,我想把这些字符串从列表中分离出来,这样列表中的每一个文本都会使其单独的行保持其标签相同。例如,我的输出数据帧应如下所示:

|Text                |Label1  |  Label2 |
----------------------------------------- 
| ['text1', 'text2', |  0     |    0 
   'text3']
-----------------------------------------
| ['text4', 'text5', |  1     |    1 
   'text6']
-----------------------------------------
    ....         ..           .. 
|  Text              |Label1  |  Label2 |
----------------------------------------- 
|  text1             |  0     |    0    |
-----------------------------------------
|  text2             |  0     |    0    |
-----------------------------------------
|  text3             |  0     |    0    |
-----------------------------------------
|  text4             |  1     |    1    |
-----------------------------------------
|  text5             |  1     |    1    |
-----------------------------------------
|  text6             |  1     |    1    |
-----------------------------------------
    ....         ..           .. 
我不知道这个问题的标题是什么。但是如何使用Pandas解决这个问题。

在数据帧上使用.explode

df = pd.DataFrame({'Text' : [['text1', 'text2', 'text3'], ['text4', 'text5', 'text6']],
                   'Label1' : [0, 1], 'Label2' : [0, 1]})

df_exploded = df.explode('Text') #explode on column 'Text'
结果:

    Text  Label1  Label2
0  text1       0       0
0  text2       0       0
0  text3       0       0
1  text4       1       1
1  text5       1       1
1  text6       1       1
使用。在数据帧上分解

df = pd.DataFrame({'Text' : [['text1', 'text2', 'text3'], ['text4', 'text5', 'text6']],
                   'Label1' : [0, 1], 'Label2' : [0, 1]})

df_exploded = df.explode('Text') #explode on column 'Text'
结果:

    Text  Label1  Label2
0  text1       0       0
0  text2       0       0
0  text3       0       0
1  text4       1       1
1  text5       1       1
1  text6       1       1
我认为这是爆炸:

我认为这是爆炸: