1 回答
TA贡献1793条经验 获得超6个赞
这是你想要的吗?
import pandas as pd
data = {'Name': ['Tom', 'nick', 'krish', 'jack', 'Kody', 'Kim'], 'Age': [20, 21, 19, 18, 6, 6],
'Size': ['M', 'M', 'L', 'S', 'S', 'M']}
data2 = {'Name': ['Jason', 'Damon', 'Ronda', 'Kylie', 'Ron', 'Harry'], 'Age': [20, 12, 11, 13, 6, 5],
'Size': ['L', 'M', 'L', 'M', 'L', 'L']}
df = pd.DataFrame(data)
df2 = pd.DataFrame(data2)
A = {}
A[0] = df
A[1] = df2
B = {}
new_df = pd.concat(A.values())
groups = new_df.groupby(["Size"])
for group in groups:
B[group[0]] = group[1]
for k, v in B.items():
print(f"{k}: {v}")
输出:
L: Name Age Size
2 krish 19 L
0 Jason 20 L
2 Ronda 11 L
4 Ron 6 L
5 Harry 5 L
M: Name Age Size
0 Tom 20 M
1 nick 21 M
5 Kim 6 M
1 Damon 12 M
3 Kylie 13 M
S: Name Age Size
3 jack 18 S
4 Kody 6 S
对于 5 个数据框(在列表中),执行以下操作:
import pandas as pd
data = {'Name': ['Tom', 'nick', 'krish', 'jack', 'Kody', 'Kim'], 'Age': [20, 21, 19, 18, 6, 6],
'Size': ['M', 'M', 'L', 'S', 'S', 'M']}
data2 = {'Name': ['Jason', 'Damon', 'Ronda', 'Kylie', 'Ron', 'Harry'], 'Age': [20, 12, 11, 13, 6, 5],
'Size': ['L', 'M', 'L', 'M', 'L', 'L']}
df = pd.DataFrame(data)
df2 = pd.DataFrame(data2)
A = {}
A[0] = df
A[1] = df2
B = []
for key, value in A.items():
groups = value.groupby(["Size"])
for group in groups:
B.append(group[1])
for x in B:
print(x)
输出:
Name Age Size
2 krish 19 L
Name Age Size
0 Tom 20 M
1 nick 21 M
5 Kim 6 M
Name Age Size
3 jack 18 S
4 Kody 6 S
Name Age Size
0 Jason 20 L
2 Ronda 11 L
4 Ron 6 L
5 Harry 5 L
Name Age Size
1 Damon 12 M
3 Kylie 13 M
添加回答
举报