您现在的位置是:亿华云 > IT科技类资讯
聊一聊十个Pandas的小技巧
亿华云2025-10-09 04:01:28【IT科技类资讯】1人已围观
简介pandas是数据科学家必备的数据处理库,我们今天总结了10个在实际应用中肯定会用到的技巧。1、Select from table where f1=’a’ and f2=’b’使用AND或OR选择子
pandas是聊聊数据科学家必备的数据处理库,我们今天总结了10个在实际应用中肯定会用到的小技技巧。
使用AND或OR选择子集
dfb = df.loc[(df.Week == week) & (df.Day == day)]OR的话是这样
dfb = df.loc[(df.Week == week)|(df.Day == day)]2、Select where in从一个df中选择一个包含在另外一个df的聊聊数据,例如下面的小技sql
select * from table1 where field1 in (select field1 from table2)我们有一个名为“days”的df,它包含以下值。聊聊
如果有第二个df:
可以直接用下面的小技方式获取
days = [0,1,2]
df[df(days)]3、Select where not in就像IN一样,聊聊我们肯定也要选择NOT IN,小技这个可能是聊聊更加常用的一个需求,但是网站模板小技却很少有文章提到,还是聊聊使用上面的数据:
days = [0,1,2]
df[~df(days)]使用~操作符就可以了
分组统计和求和也是小技常见的操作,但是聊聊使用起来并不简单
df(by=[RepID,Week,CallCycleDay]).sum()如果想保存结果或稍后使用它们并引用这些字段,请添加 as_index=False
df.groupby(by=[RepID,Week,CallCycleDay], as_index=False).sum()使用as_index= false,可以表的形式保存列。
5、从一个表更另外一个表的字段我们从一个df中更改了一些值,现在想要更新另外一个df,这个操作就很有用。
dfb = dfa[dfa.field1=somevalue].copy()
dfb[field2] = somevalue
dfa.update(dfb)这里的更新是通过索引匹配的
6、使用apply/lambda创建新字段我们创建了一个名为address的亿华云新字段,它是几个字段进行拼接的。
dfa[address] = dfa.apply(lambda row: row[StreetName] + , +7、插入新行插入新数据的最佳方法是使用concat。我们可以用有pd. datafframe .from_records一将新行转换为df。
newRow = row.copy()
newRow.CustomerID = str(newRow.CustomerID)+-+str(x)
newRow.duplicate = True
df = pd.concat([df,pd.DataFrame.from_records([newRow])])8、更改列的类型可以使用astype函数将其快速更改列的数据类型
df = pd.read_excel(customers_.xlsx)
df[Longitude] = df[Longitude].astype(str)
df[Latitude] = df[Longitude].astype(str)9、删除列使用drop可以删除列
def cleanColumns(df):
for col in df.columns:
return df10、地图上标注点这个可能是最没用的技巧,但是他很好玩。
这里我们有一些经纬度的数据。
现在我们把它根据经纬度在地图上进行标注:
df_clustercentroids = pd.read_csv(centroidFile)
lst_elements = sorted(list(dfm.cluster2.unique()))
lst_colors = [#%06X % np.random.randint(0, 0xFFFFFF) for i in range(len(lst_elements))]
dfm["color"] = dfm["cluster2"]
dfm["color"] = dfm["color"].apply(lambda x:lst_colors[lst_elements.index(x)])
m = folium.Map(locatinotallow=[dfm.iloc[0].Latitude,dfm.iloc[0].Longitude], zoom_start = 9)
for index, row in dfm.iterrows():
folium.CircleMarker(locatinotallow=[float(row[Latitude]), float(row[Longitude])],radius=4,popup=str(row[RepID]) + | +str(row.CustomerID),color=row[color],fill=True,fill_color=row[color]
).add_to(m)
for index, row in df_clustercentroids.iterrows():
folium.Marker(locatinotallow=[float(row[Latitude]), float(row[Longitude])],popup=str(index) + |#= + str(dfm.loc[dfm.cluster2==index].groupby([cluster2])[CustomerID].count().iloc[0]),icnotallow=folium.Icon(color=black,icon_color=lst_colors[index]),tooltip=str(index) + |#= + str(dfm.loc[dfm.cluster2==index].groupby([cluster2])[CustomerID].count().iloc[0])).add_to(m)
m结果如下
很赞哦!(38)
相关文章
- 域后缀首选.com,.net,然后是.cn。后缀选择不当,导致流量损失。域名是企业与互联网网址之间的链接,关键是企业在网络上存在的标志。因此,选择好域名是开展网上工作的首要重要条件。
- .com域名将全球涨价,那么cn域名注册价格是多少呢?
- cn域名好注册吗?如何在聚名网进行cn域名注册?
- cn域名好注册吗?如何在聚名网进行cn域名注册?
- 这个不用多说,不同平台的注册价格不同,且不同平台对域名释放交易的把控与曝光不同,当然价格相对便宜且平台渠道广操作便利的平台最好。
- 开源数据库SQLite、MySQL和PostgreSQL比较
- 用 Python 写的 Web 页面,如何让所有人都能访问?
- 只有一行代码的实用小工具
- 众所周知,com域名拥有最大的流通市场和流通历史。最好选择com域名,特别是在购买域名时处理域名。其次可以是cn域名、net域名、org域名等主流域名,现在比较流行的王域名和顶级域名,都是值得注册和投资的。
- 什么样的平台域名服务好?如何选择好的域名服务商?