英文:
how can i split a pandas dataframe by elements on a column?
问题
我有一个包含人员信息的Pandas数据框,我想按城市("UF")进行拆分,有没有一种更好的方法可以在不指定每个城市的情况下进行拆分?
我尝试了以下代码:
city_list = people["UF"].drop_duplicates()
temporary = []
for city in city_list:
temporary.append(people[people["UF"] == city])
英文:
i have a pandas dataframe of persons and i want to split it by city("UF"), is there a better way to do it without specifying each city?
i tried this:
city_list = people["UF"].drop_duplicates()
temporary = []
for city in city_list:
temporary.append(people[people["UF"] == city])
答案1
得分: 1
使用groupby函数,
grouped_cities = people.groupby("UF")
temporary = [group for _, group in grouped_cities]
使用groupby函数对数据进行分组,
grouped_cities = people.groupby("UF")
temporary = [group for _, group in grouped_cities]
英文:
Use groupby function,
grouped_cities = people.groupby("UF")
temporary = [group for _, group in grouped_cities]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论