自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊一聊十個Pandas的小技巧

數據庫 其他數據庫
pandas是數據科學家必備的數據處理庫,我們今天總結了10個在實際應用中肯定會用到的技巧。

pandas是數據科學家必備的數據處理庫,我們今天總結了10個在實際應用中肯定會用到的技巧。

1、Select from table where f1=’a’ and f2=’b’

使用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)]

使用~操作符就可以了

4、select sum(*) from table group by

分組統(tǒng)計和求和也是常見的操作,但是使用起來并不簡單

df(by=['RepID','Week','CallCycleDay']).sum()

如果想保存結果或稍后使用它們并引用這些字段,請?zhí)砑?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創(chuàng)建新字段

我們創(chuàng)建了一個名為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 df

10、地圖上標注點

這個可能是最沒用的技巧,但是他很好玩。

這里我們有一些經緯度的數據。

圖片

現在我們把它根據經緯度在地圖上進行標注:

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

結果如下

圖片

責任編輯:華軒 來源: DeepHub IMBA
相關推薦

2024-07-24 11:40:33

2022-09-19 16:24:33

數據可視化Matplotlib工具

2018-04-27 09:22:21

數據存儲技巧

2021-08-04 09:32:05

Typescript 技巧Partial

2019-03-21 11:04:22

安全標準信息

2023-01-09 08:48:00

IT決議結構

2024-01-30 00:40:10

2023-03-24 16:41:36

Pandas技巧數據處理

2023-07-06 13:56:14

微軟Skype

2020-09-08 06:54:29

Java Gradle語言

2013-09-29 13:36:07

虛擬SAN

2010-12-06 09:49:28

Linux快速啟動

2022-06-27 07:50:16

鏈表節(jié)點測試

2022-08-25 10:37:00

CIOIT領導者

2023-09-22 17:36:37

2021-01-28 22:31:33

分組密碼算法

2020-05-22 08:16:07

PONGPONXG-PON

2020-09-15 12:45:48

系統(tǒng)LinuxUnix

2018-06-07 13:17:12

契約測試單元測試API測試

2021-08-01 09:55:57

Netty時間輪中間件
點贊
收藏

51CTO技術棧公眾號