簡體   English   中英

將numpy數組存儲在pandas數據框的多個單元格中(Python)

[英]Store numpy array in multiples cells of pandas dataframe (Python)

我在這里很新。 我有一個這樣的熊貓數據框:

                    078401115X            0790747324            0790750708

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

我有索引,其中列為零(對於第一行):

['078401115X',
'0790747324']

現在,我試圖在熊貓數據框的那些位置中存儲零的numpy數組,無論如何我都可以直接執行此操作,而無需使用設法處理標量值的“ for循環”,但是我無法使用numpy數組來做到這一點。

非常感謝您的幫助。

具有.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]
df.loc[list_indices, column_name] = np.zeros(4)

是你想要的。 df是您的數據list_indiceslist_indices是行為0的索引列表,而np.zeros則是零列表。 如果您想要不同的課程長度,請更改4。

df.loc[list_indices, column_name]選擇在list_indices和帶有column_name的列中具有索引的行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM