import numpy as np
import pandas as pd
obj2 = pd.series([4,7,-5,3],index=['d','b','a','c'])
obj2
out[99]:
d 4
b 7
a -5
c 3
dtype: int64
a =
b = pd.series(a)
bout[102]:
a 1
b 2
c 3
dtype: int64
通過索引查單值
in [172]: series_4['a']
out[172]: 4
通過索引序列查多值:
series_4[['a','b']]
out[174]:
a 4
b 2
dtype: int64
通過布林型別索引篩選:in [175]: series_4[series_4>2]
out[175]:
a 4
c 3
dtype: int64
通過位置切片和標籤切片查詢資料:
series_4
out[194]:
a 4
b 2
c 3
dtype: int64
series_4[:2]
out[195]:
a 4
b 2
dtype: int64
series_4['a':'c']
out[196]:
a 4
b 2
c 3
dtype: int64
注意刪除的是索引值
b
out[105]:
a 1
b 2
c 3
dtype: int64
b.drop('a')
out[106]:
b 2
c 3
dtype: int64
s1
out[112]:
a 1
b 2
c 3
dtype: int64
s1['a'] ='測試'
s1out[114]:
a 測試
b 2
c 3
dtype: object
s1
out[80]:
ceshi 1
001 3
002 5
003 6
004 8
dtype: int6
s1.index._values[0] = '測試'
s1
out[87]:
測試 1
001 3
002 5
003 6
004 8
dtype: int64
值修改方法
#列印s1
s1out[94]:
0 1.0
1 3.0
2 5.0
3 nan
4 6.0
5 8.0
dtype: float64
#將索引值為0的值重新賦值
s1[0] = 11111
s1out[96]:
0 11111.0
1 3.0
2 5.0
3 nan
4 6.0
5 8.0
dtype: float64
如果索引形同,則替換,無則新增
out[117]:
a 測試
b 2
c 3
0 11111
1 3
2 5
3 nan
4 6
5 8
dtype: object
s[0] ='改變了'
sout[121]:
0 改變了
1 3
2 5
3 nan
4 6
5 8
dtype: object
out[122]:
a 測試
b 2
c 3
0 改變了
1 3
2 5
3 nan
4 6
5 8
dtype: object
>>> data =
>>> data
>>> frame = dataframe(data)
>>> frame
pop state year
0 x 1 a
1 y 2 b
>>> dataframe(data,columns=['year','pop','state'])
year pop state
0 a x 1
1 b y 2
help(df.drop)
help on method drop in module pandas.core.generic:
drop(labels=none, axis=0, index=none, columns=none, level=none, inplace=false, errors='raise') method of pandas.core.frame.dataframe instance
return new object with labels in requested axis removed.
parameters
----------
labels : single label or list-like
index or column labels to drop.
axis : int or axis name
whether to drop labels from the index (0 / 'index') or
columns (1 / 'columns').
index, columns : single label or list-like
alternative to specifying `axis` (``labels, axis=1`` is
equivalent to ``columns=labels``).
.. versionadded:: 0.21.0
level : int or level name, default none
for multiindex
inplace : bool, default false
if true, do operation inplace and return none.
errors : , default 'raise'
if 'ignore', suppress error and existing labels are dropped.
returns
-------
dropped : type of caller
examples
--------
df = pd.dataframe(np.arange(12).reshape(3,4),
columns=['a', 'b', 'c', 'd'])
dfa b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
drop columns
df.drop(['b', 'c'], axis=1)
a d
0 0 3
1 4 7
2 8 11
###['b', 'c'])中。b和c中間有個空格
df.drop(columns=['b', 'c'])
a d
0 0 3
1 4 7
2 8 11
drop a row by index
df.drop([0, 1])
a b c d
2 8 9 10 11
notes
-----
specifying both `labels` and `index` or `columns` will raise a
valueerror.
pandas學習筆記
1 建立物件,瀏覽資料 建立物件,瀏覽資料 import pandas as pd import numpy as np import matplotlib.pyplot as plt 建立series s pd.series 1,2,4,6,np.nan,9,10 index list abcde...
pandas學習筆記
1.series 類似numpy中的一維陣列,表示為索引 從0開始 和值。建立 import pandas as pd,numpy as np s1 pd.series np.arange 10 s2 pd.series 12 2,5 s3 pd.series 含有的屬性 s1.values s1....
pandas學習筆記
pandas像是python中的excel 它的基本資料結構是 在pandas中叫 dataframe 可以對資料進行各種操作和變換。它還能做很多其他的事。import pandas as pd data pd.read csv files.csv seq顯示地宣告分隔符,encodig顯示地宣告編...