擅长:python、mysql、java
<p>使用<code>groupby()</code>,然后调用它的<code>get_group()</code>方法:</p>
<pre><code>import pandas as pd
import io
text = b"""Start_Location End_Location Date
OFFICE HOME 3-Apr-15
OFFICE HOME 3-Apr-15
HOME SHOPPING 3-Apr-15
HOME SHOPPING 4-Apr-15
HOME SHOPPING 4-Apr-15
SHOPPING HOME 5-Apr-15
SHOPPING HOME 5-Apr-15
HOME SHOPPING 5-Apr-15"""
locations = ["HOME", "OFFICE", "SHOPPING"]
df = pd.read_csv(io.BytesIO(text), delim_whitespace=True)
g = df.groupby("Start_Location")
for name, df2 in g:
globals()["df_" + name.lower()] = df2
</code></pre>
<p>但我认为在for循环中添加全局变量不是一个好方法,可以通过以下方法将groupby转换为dict:</p>
^{pr2}$
<p>然后可以使用<code>d["HOME"]</code>来获取数据。在</p>