ValueError:值的长度(221)与索引的长度(189)不匹配

2024-05-12 20:09:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将列表添加到数据帧中的列中,但其中一些列表中的数据比其他列表中的数据多。我得到以下错误:

 ValueError: Length of values (221) does not match length of index (189)

每列中的数据都相互匹配,因此所有数据都在一行中匹配。有没有办法让它与数据一起工作?可能添加NaN值或null

样本清单1:

['Defense Evasion', 'Privilege Escalation', 'Defense Evasion', 'Privilege Escalation']

样本清单2:

['Windows Management Instrumentation', 'XSL Script Processing', 'Domain Policy Modification', 'Forge Web Credentials']

样本清单3:

['Blue MockingBird', 'FIN6']

预期数据帧:

Tactic                   Name                               Actor
Defense Evasion       Windows Management Instrumentation      Blue Mocking Bird
Privilege Escalation   XSL Script Processing                  
Defense Evasion        Domain Policy Modification             Fin 6
Privilege Escalation    Forge Web Credentials

Tags: of数据列表domainwindowsscriptmanagement样本
2条回答

^{}orient='index'一起使用:

In [247]: l1 = ['Defense Evasion', 'Privilege Escalation', 'Defense Evasion', 'Privilege Escalation']    
In [248]: l2 = ['Windows Management Instrumentation', 'XSL Script Processing', 'Domain Policy Modification', 'Forge Web Credentials']    
In [249]: l3 = ['Blue MockingBird', 'FIN6']

In [245]: ans = pd.DataFrame.from_dict({'Tactic':l1, 'Name': l2, 'Actor': l3}, orient='index').T

In [246]: ans
Out[246]: 
                 Tactic                                Name             Actor
0       Defense Evasion  Windows Management Instrumentation  Blue MockingBird
1  Privilege Escalation               XSL Script Processing              FIN6
2       Defense Evasion          Domain Policy Modification              None
3  Privilege Escalation               Forge Web Credentials              None

,使用^{

In [252]: ans = pd.DataFrame.from_dict({'Tactic':pd.Series(l1), 'Name': pd.Series(l2), 'Actor': pd.Series(l3)})

In [253]: ans
Out[253]: 
                 Tactic                                Name             Actor
0       Defense Evasion  Windows Management Instrumentation  Blue MockingBird
1  Privilege Escalation               XSL Script Processing              FIN6
2       Defense Evasion          Domain Policy Modification               NaN
3  Privilege Escalation               Forge Web Credentials               NaN

构造一个列为列表的数据帧,使用explode()进行扩展以获得所需的输出。在这种情况下,两个列表的长度相同,但在列表长度不相同的情况下仍然有效

df = pd.DataFrame({"Tactic":[['Defense Evasion', 'Privilege Escalation', 'Defense Evasion', 'Privilege Escalation']],
             "Name":[['Windows Management Instrumentation', 'XSL Script Processing', 'Domain Policy Modification', 'Forge Web Credentials']]})

df = df["Tactic"].explode().reset_index(drop=True).to_frame().join(df["Name"].explode().reset_index(drop=True))

print(df.to_string())

输出

                 Tactic                                Name
0       Defense Evasion  Windows Management Instrumentation
1  Privilege Escalation               XSL Script Processing
2       Defense Evasion          Domain Policy Modification
3  Privilege Escalation               Forge Web Credentials

如果要展开到列,请使用pd.Series

import numpy as np
import random
df = pd.DataFrame([{"data":np.random.randint(1,10,s), "static":"a value"} for s in np.random.randint(1,10, 10)])
df = df.join(df.data.apply(pd.Series))

输出

                          data   static    0    1    2    3    4    5    6    7    8
0                          [1]  a value  1.0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
1        [4, 2, 1, 2, 5, 4, 5]  a value  4.0  2.0  1.0  2.0  5.0  4.0  5.0  NaN  NaN
2                          [1]  a value  1.0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
3                          [2]  a value  2.0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
4     [7, 8, 4, 8, 7, 6, 2, 1]  a value  7.0  8.0  4.0  8.0  7.0  6.0  2.0  1.0  NaN
5     [1, 2, 6, 3, 9, 1, 8, 1]  a value  1.0  2.0  6.0  3.0  9.0  1.0  8.0  1.0  NaN
6        [3, 3, 9, 4, 5, 5, 7]  a value  3.0  3.0  9.0  4.0  5.0  5.0  7.0  NaN  NaN
7              [2, 8, 1, 9, 5]  a value  2.0  8.0  1.0  9.0  5.0  NaN  NaN  NaN  NaN
8  [8, 7, 7, 9, 3, 6, 1, 7, 1]  a value  8.0  7.0  7.0  9.0  3.0  6.0  1.0  7.0  1.0
9                          [7]  a value  7.0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN

相关问题 更多 >