基于它所属的组的一个熊猫列的所有排列和组合(All permutations and combinations of a pandas column based on the group it belongs to)
我有一个拥有城市名称和所属州的熊猫数据框。 我试图获得每个州的城市名称比较的所有可能组合。
示例数据框:
City State ---------- LosA Cali SanJ Cali SanF Cali Char NC Rale NC预期答案:
City1 City2 State ---------- LosA SanJ Cali LosA SanF Cali SanJ SanF Cali Char Rale NC我使用了itertools的组合,它给出了整个组合,但是有没有办法基于状态实现?
I have a pandas dataframe which has city names and the state to which they belong. I am trying to obtain all the possible combinations of city name comparisons for each state.
Example dataframe:
City State ---------- LosA Cali SanJ Cali SanF Cali Char NC Rale NCExpected Answer:
City1 City2 State ---------- LosA SanJ Cali LosA SanF Cali SanJ SanF Cali Char Rale NCI have used combinations from itertools which gives the whole combinations, but is there a way to achieve based on the State as well?
最满意答案
使用groupby + itertools.combinations的组合:
from itertools import combinations g = df.groupby('State').apply(lambda x: pd.Series(list(combinations(x.City, 2)))) df = pd.DataFrame(g.apply(list).tolist(), columns=['City1', 'City2']) df['State'] = g.index.get_level_values(0) df City1 City2 State 0 LosA SanJ Cali 1 LosA SanF Cali 2 SanJ SanF Cali 3 Char Rale NCUse a combination of groupby + itertools.combinations:
from itertools import combinations g = df.groupby('State').apply(lambda x: pd.Series(list(combinations(x.City, 2)))) df = pd.DataFrame(g.apply(list).tolist(), columns=['City1', 'City2']) df['State'] = g.index.get_level_values(0) df City1 City2 State 0 LosA SanJ Cali 1 LosA SanF Cali 2 SanJ SanF Cali 3 Char Rale NC更多推荐
发布评论