简体   繁体   English

通过来自两个不同列的唯一值汇总 df

[英]summarize df by Unique values from two different columns

say I have the following df:说我有以下df:

Origin起源 Lat纬度 Long Destination目的地 Lat纬度 Long
A一个 1 1 3 3 B 5 5 3 3
A一个 1 1 3 3 C C 7 7 3 3
B 5 5 3 3 A一个 1 1 3 3
B 5 5 3 3 C C 7 7 3 3

I need to get the df in the following shape我需要得到以下形状的df

Unique Location独特的位置 Lat纬度 Long
A一个 1 1 3 3
B 5 5 3 3
C C 7 7 3 3

is there a quick way to do that using NumPy/pandas?有没有使用 NumPy/pandas 的快速方法? I was trying to split the data into two dfs and then join them together but it seems to be like inefficient way at all.我试图将数据分成两个 dfs,然后将它们连接在一起,但这似乎是一种低效的方式。

Use pd.concat and drop_duplicates :使用pd.concatdrop_duplicates

>>> pd.concat([df.iloc[:, :3].rename(columns={'Origin': 'Unique Location'}),
               df.iloc[:, 3:].rename(columns={'Destination': 'Unique Location'})]) \
      .drop_duplicates().reset_index(drop=True)

  Unique Location  Lat  Long
0               A    1     3
1               B    5     3
2               C    7     3

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

相关问题 连接来自不同列的字符串并在 pandas df 中获取唯一值 - concat strings from different columns and get unique values in pandas df 如果两个不同数据帧中两列的值匹配,则将df2中另一列的值复制到df1中的列 - If values from two columns in two different data frames match then copy values from another column in df2 to column in df1 组合来自两个不同列的两个值并打印唯一值和唯一值计数 - combining two values from two different columns and print unique values and count of unique values 如何将两个不同列中的唯一值复制到第三列 - how to copy unique values from two different columns into third column 如何根据两个不同列的值从pandas df中删除行 - How to remove rows from pandas df based on values of two different columns 根据两个不同列中的值过滤 df 中的行 - Filtering rows in df based on values in two different columns 如何在 df 列中获取唯一值? - How to get unique values in df columns? 大熊猫包含两列的唯一值 - pandas inclusive unique values from two columns 两个如何组合两列不同的数据帧,使它们具有唯一的值? - How two combine two columns of different dataframes such that they have unique values? 如何使用“df.loc”以便它合并两列以返回“唯一”值? - How to work with ' df.loc ' so that it combines two columns to return ' unique ' values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM