如何将Pandas Data Frame列数据转换为数组值并在新列中列出

cx6n0qe3  于 2023-05-05  发布在  其他
关注(0)|答案(1)|浏览(194)

当cve列为空(NaN或NA)时,需要检查@href列数据的可用性,如果数据在@href中,则需要使数组列表中的@href和@cvss_score相同,如cve列数据格式**['@ href':'https://ubuntu.com/security/CVE','@cvss_score':'10'}]**我希望这些数据进入新列。图像-B是精确要求。

import pandas as pd

data = [{'cve': [{'@href': 'https://ubuntu.com/security/CVE','@cvss_score':'10'}],'@href': '','@cvss_score': ''},
        {'cve': '','@href': 'https://ubuntu.com/security/CVE','@cvss_score': '10'},
        {'cve': '','@href': 'https://ubuntu.com/security/CVE','@cvss_score': '10'},
        {'cve': [{'@href': 'https://ubuntu.com/security/CVE','@cvss_score':'10'}],'@href': '','@cvss_score': ''}]
df = pd.DataFrame(data)

df

图像- B(必需输出)

2guxujil

2guxujil1#

这里有一个选项:

df["ocdata"] = (df["cve"].replace("", None)
                  .fillna(pd.Series([[{"@href": h, "@cvss_score": c}]
                                     for (h,c) in zip(df["@href"], df["@cvss_score"])]))
)

输出:

print(df["ocdata"])

0    [{'@href': 'https://ubuntu.com/security/CVE', '@cvss_score': '10'}]
1    [{'@href': 'https://ubuntu.com/security/CVE', '@cvss_score': '10'}]
2    [{'@href': 'https://ubuntu.com/security/CVE', '@cvss_score': '10'}]
3    [{'@href': 'https://ubuntu.com/security/CVE', '@cvss_score': '10'}]
Name: ocdata, dtype: object

相关问题