【问题标题】:Best way to save nested list/array to CSV for easy loading later将嵌套列表/数组保存到 CSV 以便以后轻松加载的最佳方式
【发布时间】:2019-10-27 19:12:03
【问题描述】:

首先,让我为 html 表道歉。这是我能让这张歪斜的桌子看起来不错的唯一方法。

现在至于问题,我正在尝试找到将坐标数组(表中的坐标列)保存在 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>

【问题讨论】:

  • @SebastienD 但这会自行保存数组。我需要表格中的数组和其他列。
  • 对于未来的读者,我选择腌制整个数据框而不是保存到 csv

标签: python csv numpy dataframe


【解决方案1】:

我会将列存储为单独的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 数组。

【讨论】:

  • 谢谢谢尔盖。虽然必须保存数据帧和数组似乎很乏味......也许我应该腌制数据帧而不是保存到 csv?
  • 要求提供 csv 文件 ;-) 。更严重的是,numpy ndarray 是可以选择的,因此它可能是一个简单且相当有效的不错的解决方案。
  • 是的,抱歉。我是在问完这个问题后才想到腌制的。但我将腌制一个包含 numpy 数组的数据框,但数据框也是可腌制的,所以应该没问题。感谢您的关注。我将标记你对这个问题的答案,但我不会再使用它了
猜你喜欢
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多