Python pandas

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python pandas相关的知识,希望对你有一定的参考价值。

from pandas import Series,DataFrame
import pandas as pd
‘‘‘index

obj2= Series([4,7,-5,3],index=[‘d‘,‘b‘,‘a‘,‘c‘])
obj2.values
obj2.index

obj2[‘a‘]
obj2[‘c‘]

obj2[‘d‘] = 6
obj2[[‘c‘,‘d‘,‘a‘]]

obj2
obj2[obj2>0]
obj2*2
import numpy as np
np.exp(obj2)

‘‘‘ dict
‘b‘ in obj2
‘e‘ in obj2

data = {‘Ohio‘:35000,‘Texas‘:71000,‘Oregon‘:16000,‘Utah‘:5000}
obj3 = Series(data)

obj3

‘‘‘passing a dict
states = [‘California‘,‘Ohio‘,‘Oregon‘,‘Texas‘]
obj4 = Series(data,index=states)

‘‘‘missing data
pd.isnull(obj4)
pd.notnull(obj4)

‘‘‘auto align index
obj3
obj4
obj3+obj4

‘‘‘name and rename
obj4.name = ‘population‘
obj4.index.name = ‘state‘

obj4

obj2.index=[‘Bob‘,‘Helen‘,‘Jeff‘,‘Ryan‘]

‘‘‘dataframe,by default by name order
data={‘state‘:[‘Ohio‘,‘Ohio‘,‘Ohio‘,‘Nevada‘,‘Nevada‘],
‘year‘:[2000,2001,2002,2001,2002],
‘pop‘:[1.5,1.7,3.6,2.4,2.9]}

frame = DataFrame(data)

‘‘‘order by special order

DataFrame(data,columns =[‘year‘,‘state‘,‘pop‘])

‘‘‘ add one column and index
frame2= DataFrame(data,columns=[‘year‘,‘state‘,‘pop‘,‘debt‘],index=[‘one‘,‘two‘,‘three‘,‘four‘,‘five‘])

frame2.columns

‘‘‘get value by columns
frame2[‘state‘]
‘‘‘get value by index
frame2.ix[‘three‘]

‘‘‘assignment by index
frame2[‘debt‘] = 16.5
frame2[‘debt‘] = np.arange(5.)

val = Series([-1.2,-1.5,-1.7],index=[‘two‘,‘four‘,‘five‘])
frame2[‘debt‘] = val

‘‘‘del columns
frame2[‘eastern‘] = frame2.state == ‘Ohio‘
del frame2[‘eastern‘]

‘‘‘nest dict
pop={‘Nevada‘:{2001:2.4,2002:2.9},‘Ohio‘:{2000:1.5,2001:1.7,2002:3.6}}

frame3=DataFrame(pop)

‘‘‘transpose the frame

frame3.T

‘‘‘index changes different with series
DataFrame(pop,index=[2001,2002,2003])
‘‘‘dataframe get value
pdata = {‘Ohio‘:frame3[‘Ohio‘][:-1],
‘Nevada‘:frame3[‘Nevada‘][:2]}

DataFrame(pdata)

‘‘‘data frame index and column name attribute
frame3.index.name = ‘year‘;
frame3.columns.name = ‘state‘
frame3



















































































































以上是关于Python pandas的主要内容,如果未能解决你的问题,请参考以下文章

Pandas Python:KeyError 日期

Pandas Python - 计数和分组日期时间索引

KeyError:“[['','']] 中没有一个在 [columns] 中”pandas python

如何通过拆分其中的字符串将单个列拆分为多个。 -Pandas Python [重复]

如何在 Pandas Python 中合并时避免笛卡尔坐标

Pandas Python-read_csv没有读取每一行的完整数据[重复]