将numpy数组存储在pandas dataframe(Python)的倍数单元格中

时间:2016-04-29 12:25:46

标签: python numpy pandas indexing dataframe

我在这里很新。我有一个像这样的pandas数据框:

                    078401115X            0790747324            0790750708

A10ODC971MDHV8          0                     0             [(354, 1), (393, 1)]
A16CZRQL23NOIW          0              [(124, 1), (697, 1)]          0
A19ZXK9HHVRV1X          0                     0                      0

我的索引列为零(第一行):

['078401115X',
'0790747324']

现在,我正在尝试在pandas数据帧的那些位置存储numpy零的数组,无论如何直接这样做没有'for循环'我设法用标量值但是我不能这样做与numpy数组。

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:0)

df.loc[list_indices, column_name] = np.zeros(4)

是你想要的。 df是您的数据框,list_indices是行为0的索引列表,np.zeros列出零。如果你想要不同的课程长度,请更改4。

df.loc[list_indices, column_name]选择索引位于list_indices且列位为column_name的行。

答案 1 :(得分:0)

多行分配.locDataFrame尺寸匹配

以下是使用.loc零索引的完整解决方案,并克服了您的维度/长度错误

error: 'cannot set using a list-like indexer with a different length than the value'

要匹配尺寸,请在分配给零索引而不是分配原始数组时,以所需/需要的形状创建一个DataFrame个零数组。

import numpy as np
import pandas as pd
from cStringIO import StringIO

# Create example DataFrame
df_text = '''
078401115X|                                                0
0790747324|                                                0
0790750708|[(354, 1), (393, 1), (447, 1), (642, 1), (886,1)]
0800103688|                                                0
5556167281|[(41, 1), (86, 1), (341, 1), (362, 1), (419, 10)]
6300157423|                                                0
6300266850|                                                0
6301699599|                                                0
6301723465|                                                0
'''
df = pd.read_table(StringIO(df_text), sep='|', index_col=0, header=None, skipinitialspace=True)

print 'Original DataFrame:'
print df
print

# Find indexes with zero data in first column
zero_indexes = df[df[1] == '0'].index

print 'Zero Indexes:'
print zero_indexes.tolist()
print

# Assign numpy zero array to indexes
df.loc[zero_indexes] = pd.DataFrame([[np.zeros(4)]], index=zero_indexes, columns=[1])

print 'New DataFrame:'
print df
Original DataFrame:
                                                            1
0                                                            
078401115X                                                  0
0790747324                                                  0
0790750708  [(354, 1), (393, 1), (447, 1), (642, 1), (886,1)]
0800103688                                                  0
5556167281  [(41, 1), (86, 1), (341, 1), (362, 1), (419, 10)]
6300157423                                                  0
6300266850                                                  0
6301699599                                                  0
6301723465                                                  0

Zero Indexes:
['078401115X', '0790747324', '0800103688', '6300157423', '6300266850', '6301699599', '6301723465']

New DataFrame:
                                                            1
0                                                            
078401115X                               [0.0, 0.0, 0.0, 0.0]
0790747324                               [0.0, 0.0, 0.0, 0.0]
0790750708  [(354, 1), (393, 1), (447, 1), (642, 1), (886,1)]
0800103688                               [0.0, 0.0, 0.0, 0.0]
5556167281  [(41, 1), (86, 1), (341, 1), (362, 1), (419, 10)]
6300157423                               [0.0, 0.0, 0.0, 0.0]
6300266850                               [0.0, 0.0, 0.0, 0.0]
6301699599                               [0.0, 0.0, 0.0, 0.0]
6301723465                               [0.0, 0.0, 0.0, 0.0]