gpt4 book ai didi

python - 将嵌套列表/数组保存到 CSV 以便稍后加载的最佳方法

转载 作者:太空宇宙 更新时间:2023-11-03 20:46:20 25 4
gpt4 key购买 nike

首先,让我对 html 表格表示歉意。这是我能让这张倾斜的 table 看起来不错的唯一方法。

现在,对于问题,我正在尝试找到将坐标数组(表中的坐标列)保存在 csv 文件的列中以供以后使用的最佳方法。

目前,在使用 dask 读取 CSV 后,如下所示:dd.read_csv(),该列表是一个字符串表示形式,如下所示:'[[...][。 ..]..]]',我使用 ast.literal_eval 将列表的字符串表示形式转换为列表,但如果我能找到正确的保存方法,这是低效的数组。

结论:
如何将 numpy 数组保存在 CSV 列中以便稍后重新加载为 numpy 数组?或者如何从 CSV 列中提取 numpy 数组?

编辑:
我正在使用多重处理,因此下表中的每个条目首先保存为列表列表,然后在所有迭代之后将列表转换为数据帧,并保存为 CSV 文件

<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 15">
<link rel=File-List href="monte_carlo%20Jun%2013,%202019%20@%2008_files/filelist.xml">
<style id="monte_carlo Jun 13, 2019 @ 08.14__21583_Styles">
<!--table {
mso-displayed-decimal-separator: "\.";
mso-displayed-thousand-separator: " ";
}

.xl1521583 {
padding-top: 1px;
padding-right: 1px;
padding-left: 1px;
mso-ignore: padding;
color: black;
font-size: 11.0pt;
font-weight: 400;
font-style: normal;
text-decoration: none;
font-family: Calibri, sans-serif;
mso-font-charset: 0;
mso-number-format: General;
text-align: general;
vertical-align: bottom;
mso-background-source: auto;
mso-pattern: auto;
white-space: nowrap;
}

.xl6321583 {
padding-top: 1px;
padding-right: 1px;
padding-left: 1px;
mso-ignore: padding;
color: black;
font-size: 11.0pt;
font-weight: 400;
font-style: normal;
text-decoration: none;
font-family: Calibri, sans-serif;
mso-font-charset: 0;
mso-number-format: General;
text-align: center;
vertical-align: middle;
mso-background-source: auto;
mso-pattern: auto;
white-space: nowrap;
}

.xl6421583 {
padding-top: 1px;
padding-right: 1px;
padding-left: 1px;
mso-ignore: padding;
color: black;
font-size: 11.0pt;
font-weight: 400;
font-style: normal;
text-decoration: none;
font-family: Calibri, sans-serif;
mso-font-charset: 0;
mso-number-format: General;
text-align: left;
vertical-align: middle;
mso-background-source: auto;
mso-pattern: auto;
white-space: normal;
}

-->
</style>
</head>

<body>
<!--[if !excel]>&nbsp;&nbsp;<![endif]-->
<!--The following information was generated by Microsoft Excel's Publish as Web
Page wizard.-->
<!--If the same item is republished from Excel, all information between the DIV
tags will be replaced.-->
<!----------------------------->
<!--START OF OUTPUT FROM EXCEL PUBLISH AS WEB PAGE WIZARD -->
<!----------------------------->

<div id="monte_carlo Jun 13, 2019 @ 08.14__21583" align=center x:publishsource="Excel">

<table border=0 cellpadding=0 cellspacing=0 width=2451 style='border-collapse:
collapse;table-layout:fixed;width:1841pt'>
<col width=121 style='mso-width-source:userset;mso-width-alt:4425;width:91pt'>
<col width=94 style='mso-width-source:userset;mso-width-alt:3437;width:71pt'>
<col width=84 style='mso-width-source:userset;mso-width-alt:3072;width:63pt'>
<col width=416 style='mso-width-source:userset;mso-width-alt:15213;width:312pt'>
<col width=49 span=3 style='mso-width-source:userset;mso-width-alt:1792;
width:37pt'>
<col width=130 span=2 style='mso-width-source:userset;mso-width-alt:4754;
width:98pt'>
<col width=128 style='mso-width-source:userset;mso-width-alt:4681;width:96pt'>
<col width=171 span=2 style='mso-width-source:userset;mso-width-alt:6253;
width:128pt'>
<col width=169 style='mso-width-source:userset;mso-width-alt:6180;width:127pt'>
<col width=84 style='mso-width-source:userset;mso-width-alt:3072;width:63pt'>
<col width=606 style='mso-width-source:userset;mso-width-alt:22162;width:455pt'>
<tr height=20 style='height:15.0pt'>
<td height=20 class=xl1521583 width=121 style='height:15.0pt;width:91pt'>L-string</td>
<td class=xl1521583 width=94 style='width:71pt'>Coordinates</td>
<td class=xl1521583 width=84 style='width:63pt'>Area</td>
<td class=xl1521583 width=416 style='width:312pt'>Bounding Coordinates</td>
<td class=xl1521583 width=49 style='width:37pt'>% of F</td>
<td class=xl1521583 width=49 style='width:37pt'>% of +</td>
<td class=xl1521583 width=49 style='width:37pt'>% of -</td>
<td class=xl1521583 width=130 style='width:98pt'>Longest F sequence</td>
<td class=xl1521583 width=130 style='width:98pt'>Longest + sequence</td>
<td class=xl1521583 width=128 style='width:96pt'>Longest - sequence</td>
<td class=xl1521583 width=171 style='width:128pt'>Average chars between Fs</td>
<td class=xl1521583 width=171 style='width:128pt'>Average chars between +s</td>
<td class=xl1521583 width=169 style='width:127pt'>Average chars between -s</td>
<td class=xl1521583 width=84 style='width:63pt'>Angle</td>
<td class=xl1521583 width=606 style='width:455pt'>Rules</td>
</tr>
<tr height=120 style='height:90.0pt'>
<td height=120 class=xl6321583 style='height:90.0pt'>F---++F-F-+F-F-+</td>
<td class=xl6421583 width=94 style='width:71pt'>[[ 0. 0. 0.]<br>
<span style='mso-spacerun:yes'> </span>[ 0. 1. 0.]<br>
<span style='mso-spacerun:yes'> </span>[ 0.4 1.9 0.]<br>
<span style='mso-spacerun:yes'> </span>[-0.4 1.4 0.]<br>
<span style='mso-spacerun:yes'> </span>[ 0.03 0.6 0.]<br>
<span style='mso-spacerun:yes'> </span>[ 0.1 1.6 0.]]</td>
<td class=xl6321583>3.531154679</td>
<td class=xl6321583>(-0.4909271959019015, 0.0, 0.4226182617406995, 1.90630778703665)
</td>
<td class=xl6321583>0.3125</td>
<td class=xl6321583>0.25</td>
<td class=xl6321583>0.4375</td>
<td class=xl6321583>1</td>
<td class=xl6321583>2</td>
<td class=xl6321583>3</td>
<td class=xl6321583>2.25</td>
<td class=xl6321583>2.666666667</td>
<td class=xl6321583>1.166666667</td>
<td class=xl6321583>0.715584993</td>
<td class=xl6321583>{'X': {'options': ['---++X', 'F-F-+X'], 'probabilities': [0.24384925254542278, 0.7561507474545772]}}</td>
</tr>
<![if supportMisalignedColumns]>
<tr height=0 style='display:none'>
<td width=121 style='width:91pt'></td>
<td width=94 style='width:71pt'></td>
<td width=84 style='width:63pt'></td>
<td width=416 style='width:312pt'></td>
<td width=49 style='width:37pt'></td>
<td width=49 style='width:37pt'></td>
<td width=49 style='width:37pt'></td>
<td width=130 style='width:98pt'></td>
<td width=130 style='width:98pt'></td>
<td width=128 style='width:96pt'></td>
<td width=171 style='width:128pt'></td>
<td width=171 style='width:128pt'></td>
<td width=169 style='width:127pt'></td>
<td width=84 style='width:63pt'></td>
<td width=606 style='width:455pt'></td>
</tr>
<![endif]>
</table>

</div>


<!----------------------------->
<!--END OF OUTPUT FROM EXCEL PUBLISH AS WEB PAGE WIZARD-->
<!----------------------------->
</body>

</html>

最佳答案

我会将列存储为单独的 npy 文件。

我会在保存时使用:

df.assign(Coordinates=np.nan).to_csv(...)   # save all columns except coordinates to a csv file
np.save('... .npy', df['Coordinates'].values) # save coordinates as a npy file

然后在加载时:

df = pd.read_csv(...)
df['coordinates'] = np.load('... .npy')

由于 pandas 在内部使用 numpy ndarrays 作为其列,因此允许直接保存和加载包含其他 numpy 数组的对象 numpy 数组。

关于python - 将嵌套列表/数组保存到 CSV 以便稍后加载的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56576297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com