简体   繁体   English

如何按 id 将 pandas dataframe 拆分为数据帧列表?

[英]How to split pandas dataframe into list of dataframes by id?

I have a big pandas dataframe (about 150000 rows).我有一个大的 pandas dataframe(大约 150000 行)。 I have tried method groupby('id') but in returns group tuples.我试过方法 groupby('id') 但返回组元组。 I need just a list of dataframes, and then I convert them into np array batches to put into an autoencoder (like this https://www.datacamp.com/community/tutorials/autoencoder-keras-tutorial but 1D)我只需要一个数据帧列表,然后将它们转换为 np 数组批次以放入自动编码器(如https://www.datacamp.com/community/tutorials/autoencoder-keras-tutorial但 1D)

So I have a pandas dataset:所以我有一个 pandas 数据集:

data = {'Name': ['Tom', 'Joseph', 'Krish', 'John', 'John', 'John', 'John', 'Krish'], 'Age': [20, 21, 19, 18, 18, 18, 18, 18],'id': [1, 1, 2, 2, 3, 3, 3, 3]}  
# Create DataFrame  
df = pd.DataFrame(data)  
# Print the output.  
df.head(10)

I need the same output (just a list of pandas dataframe).我需要相同的 output (只是 pandas 数据帧的列表)。 Also, i need a list of unsorted lists, it is important, because its time series.另外,我需要一个未排序列表的列表,这很重要,因为它的时间序列。

data1 = {'Name': ['Tom', 'Joseph'], 'Age': [20, 21],'id': [1, 1]}  
data2 = {'Name': ['Krish', 'John', ], 'Age': [19, 18, ],'id': [2, 2]}  
data3 = {'Name': ['John', 'John', 'John', 'Krish'], 'Age': [18, 18, 18, 18],'id': [3, 3, 3, 3]}  
pd_1 = pd.DataFrame(data1)
pd_2 = pd.DataFrame(data2)
pd_3 = pd.DataFrame(data3)
array_list = [pd_1,pd_2,pd_3]
array_list

How can I split dataframe?如何拆分 dataframe?

Or you can TRY:或者您可以尝试:

array_list = df.groupby(df.id.values).agg(list).to_dict('records')

Output : Output

[{'Name': ['Tom', 'Joseph'], 'Age': [20, 21], 'id': [1, 1]},
 {'Name': ['Krish', 'John'], 'Age': [19, 18], 'id': [2, 2]},
 {'Name': ['John', 'John', 'John', 'Krish'],
  'Age': [18, 18, 18, 18],
  'id': [3, 3, 3, 3]}]

UPDATE : UPDATE

If you need a dataframe list:如果您需要 dataframe 列表:

df_list = [g for _,g in df.groupby('id')]
#OR
df_list = [pd.DataFrame(i) for i in df.groupby(df.id.values).agg(list).to_dict('records')]

To reset the index of each dataframe:要重置每个 dataframe 的索引:

df_list = [g.reset_index(drop=True) for _,g in df.groupby('id')]

Let us group on id and using to_dict with orientation list prepare records per id让我们按id group并使用带有方向listto_dict为每个id准备记录

[g.to_dict('list') for _, g in df.groupby('id', sort=False)]

[{'Name': ['Tom', 'Joseph'], 'Age': [20, 21], 'id': [1, 1]},
 {'Name': ['Krish', 'John'], 'Age': [19, 18], 'id': [2, 2]},
 {'Name': ['John', 'John', 'John', 'Krish'], 'Age': [18, 18, 18, 18], 'id': [3, 3, 3, 3]}]

I am not sure about your need but does something like this works for you?我不确定你的需要,但这样的事情对你有用吗?

df = df.set_index("id")
[df.loc[i].to_dict("list") for i in df.index.unique()]

or if you really want to keep your index in your list:或者,如果您真的想将索引保留在列表中:

[df.query(f"id == {i}").to_dict("list") for i in df.id.unique()]

If you want to create new DataFrames storing the values:如果要创建存储值的新 DataFrame:

(Previous answers are more relevant if you want to create a list) This can be solved by iterating over each id using a for loop and create a new dataframe every loop. (如果您想创建一个列表,以前的答案更相关)这可以通过使用for循环遍历每个 id 并在每个循环中创建一个新的 dataframe 来解决。 I refer you to #40498463 and the other answers for the usage of the groupby() function.关于groupby() function 的用法,我建议您参考#40498463和其他答案。 Please note that I have changed the name of the id column to Id.请注意,我已将 id 列的名称更改为 Id。

for Id, df in df.groupby("Id"):
    str1 = "df"
    str2 = str(Id)
    new_name = str1 + str2
    exec('{} = pd.DataFrame(df)'.format(new_name))

Output: Output:

df1
     Name  Age  Id
0     Tom   20   1
1  Joseph   21   1

df2
    Name  Age  Id
2  Krish   19   2
3   John   18   2

df3
    Name  Age  Id
4   John   18   3
5   John   18   3
6   John   18   3
7  Krish   18   3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM