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
使用
groupby()
,然后调用它的get_group()
方法:但我认为在for循环中添加全局变量不是一个好方法,可以通过以下方法将groupby转换为dict:
^{pr2}$然后可以使用
d["HOME"]
来获取数据。在我得到了我想要的答案
这将创建3个数据帧df_HOME、df_office和df_SHOPPING
谢谢
相关问题 更多 >
编程相关推荐