提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、pandas是什么?
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、使用步骤
1.引入库
代码如下(示例):
function flattenTree(data, parentId = null) {
const flatList = [];
data.forEach((item) => {
// 创建一个新的对象,并添加 parentId
const newItem = {
...item,
parentId: parentId,
};
// 删除 levelList,以防止嵌套
delete newItem.levelList;
// 将当前项添加到扁平化列表
flatList.push(newItem);
// 如果有子项,递归处理它们
if (item.levelList && item.levelList.length > 0) {
const children = flattenTree(item.levelList, item.uuid);
flatList.push(...children);
}
});
return flatList;
}
// 示例数据
const data = [
{
level: 1,
name: "A1",
nameEn: "A2",
uuid: "hxsuH9kn",
levelList: [
{
level: 2,
name: "A1",
nameEn: "A2",
parentLevel: "1",
uuid: "JxO2T9CT",
levelList: [
{
level: 3,
name: "A31",
nameEn: "A32",
parentLevel: "2",
uuid: "Oqh52BJX",
levelList: [],
},
{
level: 3,
name: "A1",
nameEn: "A2",
parentLevel: "2",
uuid: "w2xhjiO7",
levelList: [],
},
{
level: 3,
name: "A14",
nameEn: "A24",
parentLevel: "2",
uuid: "ySnCrey1",
levelList: [],
},
{
level: 3,
name: "A15",
nameEn: "A25",
parentLevel: "2",
uuid: "oC1Qr2sb",
levelList: [],
},
],
},
{
level: 2,
name: "A133",
nameEn: "A233",
parentLevel: "1",
uuid: "dNWKUttW",
levelList: [
{
level: 3,
name: "A3133",
nameEn: "A3233",
parentLevel: "2",
uuid: "KG5LcBk0",
levelList: [],
},
{
level: 3,
name: "A133",
nameEn: "A233",
parentLevel: "2",
uuid: "0OkAMEu3",
levelList: [],
},
{
level: 3,
name: "A143333",
nameEn: "A243333",
parentLevel: "2",
uuid: "UKQu3QBJ",
levelList: [],
},
{
level: 3,
name: "A1533",
nameEn: "A2533",
parentLevel: "2",
uuid: "h1ynAOpw",
levelList: [],
},
],
},
],
parentLevel: null,
},
];
// 扁平化数据
const flatData = flattenTree(data);
console.log(flatData);
2.读入数据
代码如下(示例):
const data = [
{
level: 1,
name: "A1",
nameEn: "A2",
uuid: "hxsuH9kn",
parentLevel: null,
parentId: null,
},
{
level: 2,
name: "A1",
nameEn: "A2",
parentLevel: "1",
uuid: "JxO2T9CT",
parentId: "hxsuH9kn",
},
{
level: 3,
name: "A31",
nameEn: "A32",
parentLevel: "2",
uuid: "Oqh52BJX",
parentId: "JxO2T9CT",
},
{
level: 3,
name: "A1",
nameEn: "A2",
parentLevel: "2",
uuid: "w2xhjiO7",
parentId: "JxO2T9CT",
},
{
level: 3,
name: "A14",
nameEn: "A24",
parentLevel: "2",
uuid: "ySnCrey1",
parentId: "JxO2T9CT",
},
{
level: 3,
name: "A15",
nameEn: "A25",
parentLevel: "2",
uuid: "oC1Qr2sb",
parentId: "JxO2T9CT",
},
{
level: 2,
name: "A133",
nameEn: "A233",
parentLevel: "1",
uuid: "dNWKUttW",
parentId: "hxsuH9kn",
},
{
level: 3,
name: "A3133",
nameEn: "A3233",
parentLevel: "2",
uuid: "KG5LcBk0",
parentId: "dNWKUttW",
},
{
level: 3,
name: "A133",
nameEn: "A233",
parentLevel: "2",
uuid: "0OkAMEu3",
parentId: "dNWKUttW",
},
{
level: 3,
name: "A143333",
nameEn: "A243333",
parentLevel: "2",
uuid: "UKQu3QBJ",
parentId: "dNWKUttW",
},
{
level: 3,
name: "A1533",
nameEn: "A2533",
parentLevel: "2",
uuid: "h1ynAOpw",
parentId: "dNWKUttW",
},
];
// 构建层级结构
const buildHierarchy = (data) => {
const map = {};
const result = [];
// 首先将每个节点按uuid存储到map中
data.forEach((item) => {
map[item.uuid] = { ...item, levelList: [] };
});
// 然后根据parentId构建层级关系
data.forEach((item) => {
if (item.parentId) {
map[item.parentId].levelList.push(map[item.uuid]);
} else {
result.push(map[item.uuid]);
}
});
return result;
};
const hierarchy = buildHierarchy(data);
console.log(hierarchy);
该处使用的url网络请求的数据。
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。