【发布时间】:2018-07-16 23:32:25
【问题描述】:
在使用和研究 pandas 模块时,我遇到了通过 pandas 计算单列中不同值的解决方案,我使用了以下代码
#!/bin/python3
import csv
import pandas as pd
## Display Settings
pd.set_option('display.height', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('expand_frame_repr', True)
## Code extraction
data = pd.read_csv('/home/karn/plura/Test/Python_Pnada/Cyber_July.csv', usecols=['Platform ID', 'Safe', 'Target system address', 'Failure reason'])
hostData = data[data['Platform ID'].str.startswith("CS-Unix-")][data['Safe'].str.contains("^CS-.*DEFAULT-UNIX-ROOT$")] [['Platform ID', 'Safe', 'Target system address','Failure reason']]
hostData.reset_index(level=0, drop=True)
safeCount = hostData.Safe.value_counts()
print(safeCount)
列的不同值的输出数据:
Safe
CS-PAR-DEFAULT-UNIX-ROOT 2
CS-MOS-DEFAULT-UNIX-ROOT 1
而代码的整个原始数据示例如下:
Platform ID Safe Target system address Failure reason
1000 CS-Unix-RootAccounts-SSH CS-PAR-DEFAULT-UNIX-ROOT jjudet First login - Unable to connect to machine. Ch...
1003 CS-Unix-RootAccounts-SSH CS-MOS-DEFAULT-UNIX-ROOT tts126 First login - Unable to connect to machine. Ch...
1005 CS-Unix-RootAccounts-SSH CS-PAR-DEFAULT-UNIX-ROOT dccamus First login - Unable to connect to machine. Ch...
注意:上面的代码都可以正常工作,但是,如果我需要从另一个用空格分隔的列中计算不同的值,例如在 Failure reason 下,我们该如何实现。
在这种情况下如何使以下工作:
safeCount = hostData."Failure reason".value_counts()
【问题讨论】:
标签: pandas python-3.6