小编Wil*_*win的帖子

使用列名称重整从长到宽

嗨,我在调整我的df时遇到麻烦。

我有:

Netflix     TV      DVD 
   0.1      0.2     0.3
   0.12     0.5     0.15
   0.4      0.6     0.8
            0.5     0.41
            0.41
            0.2 
Run Code Online (Sandbox Code Playgroud)

我想将我的df转换为:

Netflix  [0.1, 0.12, 0.4]
TV       [0.2, 0.5, 0.6, 0.5, 0.41, 0.2] 
DVD      [0.3, 0.15, 0.8, 0.41]
Run Code Online (Sandbox Code Playgroud)

不知道stack()ivot()如何在这种df上工作。任何帮助表示赞赏。

python numpy reshape pandas

7
推荐指数
2
解决办法
129
查看次数

熊猫从列中删除空格以在列上创建

我有一些数据看起来像

pd.DataFrame([
    {'col1': 'x', 'col2': 'name_x', 'col3': '', 'col4': '', 'col5': '', 'col6': ''},
    {'col1':'', 'col2':'', 'col3': 'y', 'col4':'name_y', 'col5': '', 'col6':''},
    {'col1':'', 'col2':'', 'col3': 'yy', 'col4':'name_yy', 'col5': '', 'col6':''},
    {'col1':'', 'col2':'', 'col3': '', 'col4':'', 'col5': 'z', 'col6':'name_z'},
    {'col1':'xx', 'col2':'name_xx', 'col3': '', 'col4':'', 'col5': '', 'col6':''},
    {'col1':'', 'col2':'', 'col3': 'yyy', 'col4':'name_yyy', 'col5': '', 'col6':''}
])

   col1  col2   col3    col4    col5    col6
0   x   name_x              
1                y     name_y       
2                yy    name_yy      
3                                z  name_z
4   xx  name_xx             
5                yyy   name_yyy         
Run Code Online (Sandbox Code Playgroud)

我需要将所有数据推送到最左边的列,即。 …

python pandas

4
推荐指数
1
解决办法
33
查看次数

嵌套列表操作

我有一个嵌套列表,其中包含产品名称,产品ID和产品数据(另一个列表)。我想创建一个具有唯一名称和产品ID的词典列表,并加入与名称和ID变量相关的数据列表。

我的数据如下:

print(x)
[(14, 'netflix', [1, 2, 3, 4, 5, 6]), (14, 'netflix', [7, 8, 9, 10]),(15,'tv',
[1, 2, 3, 4, 5]), (15, 'tv', [7, 8, 9]), (16, 'radio', [1, 2, 3, 4]), 
(16, 'radio', [1, 2, 3, 4]) 
Run Code Online (Sandbox Code Playgroud)

我想将数据转换为:

x = [{'product_id':x[0], 'product': row[1], 'values':row[2]} for row in x]

#or for this example

x = [{'product_id':14, 'product':'netflix', 'values':[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}, 
     {'product_id':15, 'product': 'tv',     'values':[1, 2, 3, 4, 5, 6, 7, 8, 9]}, …
Run Code Online (Sandbox Code Playgroud)

python numpy list python-3.x pandas

3
推荐指数
1
解决办法
130
查看次数

标签 统计

pandas ×3

python ×3

numpy ×2

list ×1

python-3.x ×1

reshape ×1