在C#中,可以使用第三方库如NumSharp或者ML.NET来使用DataFrame进行数据分析。
使用NumSharp库:
using NumSharp; using NumSharp.Extensions; // 创建DataFrame var data = https://www.yisu.com/ask/new DataFrame();"Name"] = new string[] { "Alice", "Bob", "Charlie", "David" }; data["Age"] = new int[] { 25, 30, 35, 40 }; data["Salary"] = new int[] { 50000, 60000, 70000, 80000 }; // 访问DataFrame的列 var names = data["Name"].ToStringArray(); var ages = data["Age"].ToInt32Array(); var salaries = data["Salary"].ToInt32Array(); // 进行数据分析操作 var averageSalary = data["Salary"].Mean(); var maxAge = data["Age"].Max();
使用ML.NET库:
using Microsoft.ML; using Microsoft.ML.Data; // 定义数据模型 public class EmployeeData { [LoadColumn(0)] public string Name { get; set; } [LoadColumn(1)] public float Age { get; set; } [LoadColumn(2)] public float Salary { get; set; } } // 创建MLContext var mlContext = new MLContext(); // 加载数据 var data = https://www.yisu.com/ask/mlContext.Data.LoadFromEnumerable(new EmployeeData[] { new EmployeeData { Name = "Alice", Age = 25, Salary = 50000 }, new EmployeeData { Name = "Bob", Age = 30, Salary = 60000 }, new EmployeeData { Name = "Charlie", Age = 35, Salary = 70000 }, new EmployeeData { Name = "David", Age = 40, Salary = 80000 } }); // 进行数据转换操作 var transformedData = https://www.yisu.com/ask/mlContext.Data.CreateEnumerable (data, reuseRowObject: false); // 进行数据分析操作 var averageSalary = transformedData.Select(x => x.Salary).Average(); var maxAge = transformedData.Select(x => x.Age).Max();
以上是使用NumSharp和ML.NET库进行DataFrame数据分析的简单示例。可以根据具体的需求和数据进行更详细的操作和分析。