我有一个熊猫系列,目前看起来像这样:
14    [Yellow, Pizza, Restaurants]
...
160920                  [Automotive, Auto Parts & Supplies]
160921       [Lighting Fixtures & Equipment, Home Services]
160922                 [Food, Pizza, Candy Stores]
160923           [Hair Removal, Nail Salons, Beauty & Spas]
160924           [Hair Removal, Nail Salons, Beauty & Spas]
我希望从根本上将其重塑为一个看起来像这样的数据框......
      Yellow  Automotive  Pizza
14       1         0        1
…           
160920   0         1        0
160921   0         0        0
160922   0         0        1
160923   0         0        0
160924   0         0        0
即.一个逻辑结构,指出每个观察(行)属于哪些类别.
我能够编写基于循环的代码来解决这个问题,但考虑到我需要处理大量的行,这将是非常缓慢的.
有谁知道这种问题的矢量化解决方案?我会非常感激的.
编辑:有509个类别,我有一个列表.
我使用的是python2.7和pandas 0.11.0.
我尝试使用DataFrame.apply(func)填充数据框的列.func()函数应该返回一个numpy数组(1x3).
import pandas as pd
import numpy as np
df= pd.DataFrame(np.random.randn(4, 3), columns=list('ABC'))
print(df)
              A         B         C
    0  0.910142  0.788300  0.114164
    1 -0.603282 -0.625895  2.843130
    2  1.823752 -0.091736 -0.107781
    3  0.447743 -0.163605  0.514052
用于测试目的的功能:
def test(row):
   # some complex calc here 
   # based on the values from different columns 
   return np.array((1,2,3))
df['D'] = df.apply(test, axis=1)
[...]
ValueError: Wrong number of items passed 1, indices imply 3
有趣的是,当我从头开始创建数据框时,它工作得很好,并按预期返回:
dic = {'A': {0: 0.9, 1: -0.6, 2: 1.8, 3: 0.4}, …我对深度学习和计算机视觉非常陌生。我想做一些人脸识别项目。为此,我从互联网上下载了一些图像,并借助张量流文档中的本文将其转换为张量流数据集。现在我想将该数据集转换为 pandas 数据框,以便将其转换为 csv 文件。我尝试了很多但无法做到。有人可以帮我吗?这是制作数据集的代码,然后是我为此尝试的一些错误代码。
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
filenames = tf.constant(['al.jpg', 'al2.jpg', 'al3.jpg', 'al4.jpeg','al5.jpeg', 'al6.jpeg','al7.jpg','al8.jpeg', '5.jpg', 'hrit8.jpeg', 'Hrithik-Roshan.jpg', 'Hrithik.jpg', 'hriti1.jpeg', 'hriti2.jpg', 'hriti3.jpeg', 'hritik4.jpeg', 'hritik5.jpg', 'hritk9.jpeg', 'index.jpeg', 'sah.jpeg', 'sah1.jpeg', 'sah3.jpeg', 'sah4.jpg', 'sah5.jpg','sah6.jpg','sah7.jpg'])
labels = tf.constant([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2])
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
def …