python - 如何从现有数据集中生成一组新值?

标签 python r python-3.x

这是一个非常广泛的问题,但我希望有人可以帮助我解决这个问题。

我有一个大数据集,我需要从中生成一些示例。生成的数据应包含可能出现在原始数据集中的值(不是随机的,但基于原始数据集仍然有意义的值)。例如:

Original Data Set

Vendor       |  Category      |  Problem Category      | Initial Detection Date | Action Date          | Affected Devices    |  Engineer | Type of Analysis | Priority

M-Operations |  Cybersecurity |  Bad security policies |   09/11/2017 12:00AM   |   09/11/2017 3:00AM  | Third party devices | John O.   | Hourly Analysis  | P1 
M-Operations |  Cybersecurity |  Penetration breach    |   09/11/2017 12:00AM   |   09/12/2017 3:00AM  | Third party devices | Samuel P. | Daily Analysis   | P2
Secure Total |  CERT          |  Bad security policies |   09/13/2017 12:00AM   |   09/13/2017 3:00AM  | Main frames         | Samuel P. | Hourly Analysis  | P1


New Data Set

Vendor       |  Category      |  Problem Category      | Initial Detection Date | Action Date          | Affected Devices    |  Engineer | Type of Analysis | Priority

M-Operations |  Cybersecurity |  Penetration breach    |   09/20/2017 12:00AM   |   09/21/2017 3:00AM  | Main frames    | John P.   | Hourly Analysis  | P1 
M-Operations |  CERT          |  Bad security policies |   09/23/2017 12:00AM   |   09/23/2017 3:00AM  | Mobile devices | Samuel P. | Daily Analysis   | P3
Secure Total |  CERT          |  Bad security policies |   09/29/2017 12:00AM   |   09/30/2017 3:00AM  | Main frames    | John P.   | Hourly Analysis  | P2

我找到了以下链接Generate data by using existing data set这与我的问题类似,但在这种情况下,所有值都是数字,而在我的示例中,我的一些值是非数字的。如果有人能为我提供一个如何用 Python 或 R 生成这个新数据集的示例,我将非常感激,因为我内心平静。

最佳答案

这是在 R 中执行此操作的方法

# Create dataframe
original <- data.frame(Vendor = c("M-Operations", "M-Operations", "Secure Total"), 
                       Category = c("Cybersecurity", "CERT", "CERT"),
                       Problem = c("Bad", "Good", "Good"))

# Create a list of all unique values in each column
l <- list(
  Vendor <- unique(original$Vendor), 
  Category <- unique(original$Category), 
  Problem <- unique(original$Problem))

# Find all possible permutations
new.data <- do.call(expand.grid, l)

# assign names to the new dataset
names(new.data) <- c("Vendor", "Category", "Problem")
new.data
#         Vendor      Category Problem
# 1 M-Operations Cybersecurity     Bad
# 2 Secure Total Cybersecurity     Bad
# 3 M-Operations          CERT     Bad
# 4 Secure Total          CERT     Bad
# 5 M-Operations Cybersecurity    Good
# 6 Secure Total Cybersecurity    Good
# 7 M-Operations          CERT    Good
# 8 Secure Total          CERT    Good

之后,您可以按照您想要的任何方式对此数据帧进行子集化,并根据测试需要保留尽可能多的记录

关于python - 如何从现有数据集中生成一组新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50206349/

相关文章:

r - 使用 dplyr::tbl 引用 PostgreSQL(振幅分析)表失败

python - 尝试解码国际化网址

Python - 所有组合包括括号

查找列表的最大绝对值的 Pythonic 方法

python - 如何解决类型错误: 'numpy.ndarray' object is not callable on Python

python - 在numpy中将非常低的值设置为零

r - 如何在 igraph 中提取社区的边缘列表?

r - 向图中添加额外的 X 轴和一些线条/注释以显示其下方数据的百分比

python - 如何在OpenERP中连接到不同的数据库?

python - 如何选择限制列数据框的数量? PYTHON