how can i split a pandas dataframe by elements on a column?

huangapple go评论66阅读模式
英文:

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]

huangapple
  • 本文由 发表于 2023年8月8日 23:44:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定