Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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 - Fatal编程技术网

Python 如何从不同长度的列表列表中创建数据帧?

Python 如何从不同长度的列表列表中创建数据帧?,python,pandas,dataframe,Python,Pandas,Dataframe,我有如下格式的数据 data = [["a", "b", "c"], ["b", "c"], ["d", "e", "f", "c"]] 我希望有一个数据帧,它包含所有唯一的字符串作为列,以及出现的二进制值 a b c d e f 0 1 1 1 0 0 0 1 0 1 1 0 0 0 2 0 0 1 1 1 1 我有一个使用列表理解的工作代码,但是对于大数据来说它非常慢 # vocab_list c

我有如下格式的数据

data = [["a", "b", "c"],
        ["b", "c"],
        ["d", "e", "f", "c"]]
我希望有一个数据帧,它包含所有唯一的字符串作为列,以及出现的二进制值

    a  b  c  d  e  f
0   1  1  1  0  0  0
1   0  1  1  0  0  0
2   0  0  1  1  1  1
我有一个使用列表理解的工作代码,但是对于大数据来说它非常慢

# vocab_list contains all the unique keys, which is obtained when reading in data from file
df = pd.DataFrame([[1 if word in entry else 0 for word in vocab_list] for entry in data])
有没有办法优化这项任务?谢谢

编辑(实际数据的小样本):

[a', “关于”, "荒谬",, “再来一次”, "安",, “合伙人”, “写”, “写”, “x”, “约克”, “你”, “您的”], ['a', "持久",, “年龄”, "加重",, "咄咄逼人",, “全部”, “差不多”, “独自一人”, "已经",, "也",,
“尽管”]

为了获得更好的性能,请使用:

编辑:

纯熊猫解决方案是可能的,但我想它应该更慢:

df = pd.get_dummies(pd.DataFrame(data), prefix='', prefix_sep='').max(level=0, axis=1)
print (df)
   a  b  d  c  e  f
0  1  1  0  1  0  0
1  0  1  0  1  0  0
2  0  0  1  1  1  1

df = pd.get_dummies(pd.DataFrame(data), prefix='', prefix_sep='').max(level=0, axis=1)
print (df)
   a  abiding  about  absurd  age  again  aggravated  aggressively  an  all  \
0  1        0      1       1    0      1           0             0   1    0   
1  1        1      0       0    1      0           1             1   0    1   

   ...  writes  alone  wrote  already  x  also  york  although  you  your  
0  ...       1      0      1        0  1     0     1         0    1     1  
1  ...       0      1      0        1  0     1     0         1    0     0  

[2 rows x 22 columns]

要获得更好的性能,请使用:

编辑:

纯熊猫解决方案是可能的,但我想它应该更慢:

df = pd.get_dummies(pd.DataFrame(data), prefix='', prefix_sep='').max(level=0, axis=1)
print (df)
   a  b  d  c  e  f
0  1  1  0  1  0  0
1  0  1  0  1  0  0
2  0  0  1  1  1  1

df = pd.get_dummies(pd.DataFrame(data), prefix='', prefix_sep='').max(level=0, axis=1)
print (df)
   a  abiding  about  absurd  age  again  aggravated  aggressively  an  all  \
0  1        0      1       1    0      1           0             0   1    0   
1  1        1      0       0    1      0           1             1   0    1   

   ...  writes  alone  wrote  already  x  also  york  although  you  your  
0  ...       1      0      1        0  1     0     1         0    1     1  
1  ...       0      1      0        1  0     1     0         1    0     0  

[2 rows x 22 columns]

您可以在列表理解中使用
join
,并且:

[外]


您可以在列表理解中使用
join
,并且:

[外]


出于好奇,我对建议的解决方案进行了计时:

from string import ascii_letters
from random import choice, randint
from datetime import datetime
import pandas as pd
from sklearn.preprocessing import MultiLabelBinarizer

data = []
for _ in range(10000):
    data.append([choice(ascii_letters) for _ in range(randint(25, 65))])

print("Time using 'pd.Series' and 'str.get_dummies()':")
startTime = datetime.now()
df = pd.Series(['|'.join(x) for x in data]).str.get_dummies()
print(datetime.now() - startTime)

print("Time using 'pd.get_dummies()':")
startTime = datetime.now()
df2 = pd.get_dummies(pd.DataFrame(data), prefix='', prefix_sep='').max(level=0, axis=1)
print(datetime.now() - startTime)

print("Time using 'MultiLabelBinarizer()':")
startTime = datetime.now()
mlb = MultiLabelBinarizer()
df3 = pd.DataFrame(mlb.fit_transform(data),columns=mlb.classes_)
print(datetime.now() - startTime)
虽然每次使用随机长度的列表时,结果都会有所不同,但差异或多或少是相同的:

Time using 'pd.Series' and 'str.get_dummies()':
0:00:00.450311
Time using 'pd.get_dummies()':
0:00:00.498003
Time using 'MultiLabelBinarizer()':
0:00:00.083955

因此,事实上,使用sklearn可以获得更快的结果。

出于好奇,我对建议的解决方案进行了计时:

from string import ascii_letters
from random import choice, randint
from datetime import datetime
import pandas as pd
from sklearn.preprocessing import MultiLabelBinarizer

data = []
for _ in range(10000):
    data.append([choice(ascii_letters) for _ in range(randint(25, 65))])

print("Time using 'pd.Series' and 'str.get_dummies()':")
startTime = datetime.now()
df = pd.Series(['|'.join(x) for x in data]).str.get_dummies()
print(datetime.now() - startTime)

print("Time using 'pd.get_dummies()':")
startTime = datetime.now()
df2 = pd.get_dummies(pd.DataFrame(data), prefix='', prefix_sep='').max(level=0, axis=1)
print(datetime.now() - startTime)

print("Time using 'MultiLabelBinarizer()':")
startTime = datetime.now()
mlb = MultiLabelBinarizer()
df3 = pd.DataFrame(mlb.fit_transform(data),columns=mlb.classes_)
print(datetime.now() - startTime)
虽然每次使用随机长度的列表时,结果都会有所不同,但差异或多或少是相同的:

Time using 'pd.Series' and 'str.get_dummies()':
0:00:00.450311
Time using 'pd.get_dummies()':
0:00:00.498003
Time using 'MultiLabelBinarizer()':
0:00:00.083955

因此,事实上,使用sklearn可以获得更快的结果。

对于mlb解决方案,有没有办法将列与vocab_列表相匹配?对于我设计的这个小示例,它似乎运行得很好,但对于列表中具有较长字符串的实际数据,列显示为奇怪的标签,如“aaaa”和“aaallll”。我已经添加了一个我实际数据的例子。啊,这只是我的自以为是。这些奇怪的标签在我的数据中变成了奇怪的单词。感谢您的确认。对于mlb解决方案,有没有办法将这些列重新匹配到vocab_列表?对于我设计的这个小示例,它似乎运行得很好,但对于列表中具有较长字符串的实际数据,列显示为奇怪的标签,如“aaaa”和“aaallll”。我已经添加了一个我实际数据的例子。啊,这只是我的自以为是。这些奇怪的标签在我的数据中变成了奇怪的单词。谢谢你的确认。
Time using 'pd.Series' and 'str.get_dummies()':
0:00:00.450311
Time using 'pd.get_dummies()':
0:00:00.498003
Time using 'MultiLabelBinarizer()':
0:00:00.083955