joyful pandas task6-连接

1.值连接
merge函数:
1.左连接:df1.merge(df2, on=‘Name’, how=‘left’)
2.不同列名:df1.merge(df2, left_on=‘df1_name’, right_on=‘df2_name’, how=‘left’)
3.如果两个表中的列出现了重复的列名,那么可以通过suffixes参数指定。
df1.merge(df2, on=‘Name’, how=‘left’, suffixes=[’_Chinese’,’_Math’])
4.在某些时候出现重复元素是麻烦的,例如两位同学来自不同的班级,但是姓名相同,这种时候就要指定on参数为多个列使得正确连接。
df1.merge(df2, on=[‘Name’, ‘Class’], how=‘left’) # 正确的结果
5.duplicated检查是否重复;merge中也提供了validate参数来检查连接的唯一性模式。

2.索引连接
1.join函数来处理索引连接
df1.join(df2, how=‘left’)
df1.join(df2, how=‘left’, lsuffix=’_Chinese’, rsuffix=’_Math’)

3.方向连接
1.concat,默认状态下的axis=0,表示纵向拼接多个表,常常用于多个样本的拼接;而axis=1表示横向拼接多个表,常用于多个字段或特征的拼接。
pd.concat([df1, df2])
pd.concat([df1, df2, df3], 1)
通过keys参数产生多级索引进行标记:pd.concat([df1, df2], keys=[‘one’, ‘two’])

2.序列与表的合并
df1.append(s, ignore_index=True)
df1.assign(Grade=s)

4.类连接操作
1.比较
df1.compare(df2, keep_shape=True)
2.组合
combine函数能够让两张表按照一定的规则进行组合,在进行规则比较时会自动进行列索引的对齐。
def choose_min(s1, s2):
s2 = s2.reindex_like(s1)
res = s1.where(s1<s2, s2)
res = res.mask(s1.isna()) # isna表示是否为缺失值,返回布尔序列
return res
df1 = pd.DataFrame({‘A’:[1,2], ‘B’:[3,4], ‘C’:[5,6]})
df2 = pd.DataFrame({‘B’:[5,6], ‘C’:[7,8], ‘D’:[9,10]}, index=[1,2])
df1.combine(df2, choose_min)

转:joyful pandas

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值