Pandas學習打卡

2021-10-05 14:08:07 字數 4405 閱讀 6977

import numpy as np

import pandas as pd

df = pd.read_csv(

"f:/data/drugs.csv"

,index_col =

['state'

,'county'])

.sort_index(

)result = pd.pivot_table(df,index=

['state'

,'county'

,'substancename'

],columns=

'yyyy'

,values=

'drugreports'

,fill_value=

'-')

.reset_index(

).rename_axis(columns=

)result.head(

)

state

county

substancename

2010

2011

2012

2013

2014

2015

2016

20170ky

adair

buprenorphine-3

54275

7101ky

adair

codeine--

1---

-12ky

adair

fentanyl--

1---

--3ky

adair

heroin--

12-1

-24ky

adair

hydrocodone69

101097

113

result_melted = result.melt(id_vars=result.columns[:3

],value_vars=result.columns[-8

:],var_name=

'yyyy'

,value_name=

'drugreports'

).query(

'drugreports != "-"'

)result2 = result_melted.sort_values(by=

['state'

,'county'

,'yyyy'

,'substancename'])

.reset_index(

).drop(columns=

'index'

)cols =

list

(result2.columns)

a, b = cols.index(

'substancename'

), cols.index(

'yyyy'

)cols[b]

, cols[a]

= cols[a]

, cols[b]

result2 = result2[cols]

.astype(

)result2.head(

)result2.head(

)

state

county

yyyy

substancename

drugreports0ky

adair

2010

hydrocodone61

kyadair

2010

methadone12

kyadair

2011

buprenorphine33

kyadair

2011

hydrocodone94

kyadair

2011

morphine

2

df_tidy = df.reset_index(

).sort_values(by=result2.columns[:4

].tolist())

.reset_index(

).drop(columns=

'index'

)df_tidy.equals(result2)

false
df = pd.read_csv(

'f:data/earthquake.csv'

)df = df.sort_values(by=df.columns.tolist()[

:3])

.sort_index(axis=1)

.reset_index(

).drop(columns=

'index'

)result = pd.pivot_table(df,index=

['日期'

,'時間'

,'維度'

,'經度'

],columns=

'方向'

,values=

['烈度'

,'深度'

,'距離'

],fill_value=

'-')

.stack(level=0)

.rename_axis(index=

)result.head(

6)

方向east

north

north_east

north_west

south

south_east

south_west

west

日期時間

維度經度

**引數

1912.08.09

12:29:00 am

40.6

27.2深度-

----

16--烈度

----

-6.7--

距離---

--4.3-

-1912.08.10

12:23:00 am

40.6

27.1深度-

----

-15-烈度

----

--6-

距離---

---2

-

df_result = result.unstack(

).stack(0)

[(~(result.unstack(

).stack(0)

=='-'))

.any(1

)].reset_index(

)df_result.columns.name=

none

df_result = df_result.sort_index(axis=1)

.astype(

)df_result.head(

)

方向日期

時間深度

烈度經度

維度距離

0south_east

1912.08.09

12:29:00 am

16.0

6.727.2

40.6

4.31

south_west

1912.08.10

12:23:00 am

15.0

6.027.1

40.6

2.02

south_west

1912.08.10

12:30:00 am

15.0

5.227.1

40.6

2.03

south_east

1912.08.11

12:19:04 am

30.0

4.927.2

40.6

4.34

south_west

1912.08.11

12:20:00 am

15.0

4.527.1

40.6

2.0

df_result.astype(

,copy=

false

).dtypes

方向     object

日期 object

時間 object

深度 float64

烈度 float64

經度 float64

維度 float64

距離 float64

dtype: object

df.equals(df_result)
true

pandas打卡學習之資料重構

有時候,用於處理的資料可能是分塊儲存的,有可能儲存在多個檔案中。這時候,就需要使用連線方法pd.concat 進行連線。concat 方法需要指定軸,預設是y軸連線,如果需要x軸連線,可以指定axis 1。concat使用的是等值連線,鍵預設為第一列,也可以指定。如果兩列表資料沒有共同的列,也會成功...

pandas 打卡第三天

1np.r 在下方堆疊 np.c 在一側堆疊import pandas as pd import numpy as np arr np.arange 6 arr1 arr.reshape 3,2 arr2 np.random.randn 3,2 print np.r arr1,arr2 print ...

PANDAS第二次打卡

第2章 索引 import numpy as np import pandas as pd df pd.read csv data table.csv index col id df.head school class gender address height weight math physic...