Hi i got the dataframe like this
data = [(1,"tom", 23),
(1,"nick", 12),
(1,"jim",24),
(2,"tom", 44),
(2,"nick", 56),
(2,"jim",77),
(3, "tom", 88),
(3, "nick", 10),
(3, "jim", 13),
]
df = pd.DataFrame(data,columns=['class', 'Name','Number'])
output of the dataframe
class Name Number
0 1 tom 23
1 1 nick 12
2 1 jim 24
3 2 tom 44
4 2 nick 56
5 2 jim 77
6 3 tom 88
7 3 nick 10
8 3 jim 13
i want to loop and get a new dataframe with the same class. The output should be like this
class Name Number
0 1 tom 23
1 1 nick 12
2 1 jim 24
max_number_class_1 = 23
class Name Number
3 2 tom 44
4 2 nick 56
5 2 jim 77
max_number_class_2 = 77
class Name Number
6 3 tom 88
7 3 nick 10
8 3 jim 13
max_number_class_3 = 88
Thank you very much for helping me !
CodePudding user response:
You could just filter using
dfs = [df[df['class'].eq(key)] for key in df['class'].unique()]
This gives you the desired result as a list.
CodePudding user response:
It's not very neat but hopefully will serve the purpose.
df_list= []
for i in df['class'].unique():
grouped = df.groupby(['class'])
df_class= grouped.get_group(i)
df_list.append(df_class)
for j in range (len(df_list)):
print(df_list[j])
print('The Maximum number in this class is',df_list[j]['Number'].max())
print('The minimum number in this class is',df_list[j]['Number'].min())
print()
The output looks like this:
class Name Number
0 1 tom 23
1 1 nick 12
2 1 jim 24
The Maximum number in this class is 24
The minimum number in this class is 12
class Name Number
3 2 tom 44
4 2 nick 56
5 2 jim 77
The Maximum number in this class is 77
The minimum number in this class is 44
class Name Number
6 3 tom 88
7 3 nick 10
8 3 jim 13
The Maximum number in this class is 88
The minimum number in this class is 10
CodePudding user response:
Here are a few ways:
1- List Comprehension
dfs = [df[df["Class"].eq(x)] for x in df["Class"].unique()]
for df in dfs:
max_number = df.Number.max()
class_number = df["Class"].head(1).squeeze()
print(f"{df}\n")
print(f"max_number_class_{class_number} = {max_number}\n")
Class Name Number
0 1 tom 23
1 1 nick 12
2 1 jim 13
max_number_class_1 = 23
Class Name Number
3 2 tom 44
4 2 nick 56
5 2 jim 77
max_number_class_2 = 77
Class Name Number
6 3 tom 88
7 3 nick 10
8 3 jim 13
max_number_class_3 = 88
2- Dictionary Comprehension
df_mapping = {x: df[df["Class"].eq(x)] for x in df["Class"].unique()}
for key in df_mapping.keys():
max_number = df_mapping[key].Number.max()
class_number = df_mapping[key]["Class"].head(1).squeeze()
print(f"{df_mapping[key]}\n")
print(f"max_number_class_{class_number} = {max_number}\n")
Class Name Number
0 1 tom 23
1 1 nick 12
2 1 jim 13
max_number_class_1 = 23
Class Name Number
3 2 tom 44
4 2 nick 56
5 2 jim 77
max_number_class_2 = 77
Class Name Number
6 3 tom 88
7 3 nick 10
8 3 jim 13
max_number_class_3 = 88
3- Numpy Split
dfs = np.split(df.sort_values("Class", ascending=True), len(df["Name"].unique()))
for df in dfs:
max_number = df.Number.max()
class_number = df["Class"].head(1).squeeze()
print(f"{df}\n")
print(f"max_number_class_{class_number} = {max_number}\n")
Class Name Number
0 1 tom 23
1 1 nick 12
2 1 jim 13
max_number_class_1 = 23
Class Name Number
3 2 tom 44
4 2 nick 56
5 2 jim 77
max_number_class_2 = 77
Class Name Number
6 3 tom 88
7 3 nick 10
8 3 jim 13
max_number_class_3 = 88