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

时间:2018-05-07 02:29:22

标签: 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生成这个新数据集的例子,我将非常感激我的内心平静。

1 个答案:

答案 0 :(得分:2)

这是你在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

之后,您可以按照您希望保留测试所需的多个记录的方式对此数据框进行子集化