<h3>将常量值分配给数据帧列</h3>
<p>如果第一个数组始终是单行数据帧或一维数组,则可以使用<code>pandas</code>为列指定一个常量值</p>
<p>语法是<code>my_dataframe["new_column"] = constant_value</code></p>
<p>因为<code>arr1</code>是一个数据帧,访问一个列将给我们一个系列。为了得到它的常量值,我们需要将单元格中的值索引为0-或第一行</p>
<p>在您的情况下,这将成为:</p>
<pre class="lang-py prettyprint-override"><code>>>> type(arr1), type(arr2)
(pandas.core.frame.DataFrame, pandas.core.frame.DataFrame)
>>> arr2["year"] = arr1["year"].loc[0]
>>> arr2["month"] = arr1["month"].loc[0]
>>> arr2["day"] = arr1["day"].loc[0]
>>> arr2
hours minutes seconds year month day
0 9 6 22.001464 2019 11 9
1 8 21 28.412044 2019 11 9
2 10 7 22.433552 2019 11 9
3 18 37 19.551359 2019 11 9
4 19 1 40.722019 2019 11 9
.. ... ... ... ... ... ...
95 2 16 48.368643 2019 11 9
96 19 22 25.034936 2019 11 9
97 10 0 20.163870 2019 11 9
98 16 35 27.251357 2019 11 9
99 8 26 54.200897 2019 11 9
</code></pre>
<p>请记住,修改<code>arr2</code>对象将在适当的位置工作</p>
<h4>访问数据帧后面的numpy阵列</h4>
<p>如果需要多维数组,只需调用:</p>
<pre class="lang-py prettyprint-override"><code>>>> arr2_np = arr2.to_numpy()
</code></pre>
<h4>根据您的用例对列进行排序
<p>如果需要对列进行排序,您可以对它们进行不同的查看,如下所示:</p>
<pre class="lang-py prettyprint-override"><code>>>> cols = arr2.columns.to_list()
>>> cols2 = cols[3:] + cols[:3]
>>> arr2[cols2]
year month day hours minutes seconds
0 2019 11 9 9 6 22.001464
1 2019 11 9 8 21 28.412044
2 2019 11 9 10 7 22.433552
3 2019 11 9 18 37 19.551359
4 2019 11 9 19 1 40.722019
.. ... ... ... ... ... ...
95 2019 11 9 2 16 48.368643
96 2019 11 9 19 22 25.034936
97 2019 11 9 10 0 20.163870
98 2019 11 9 16 35 27.251357
99 2019 11 9 8 26 54.200897
</code></pre>