Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Dataframes in Pandas

SeriesPandas
In [7]:
print("hello")
hello
In [2]:
from pandas import Series
defaultSeries = Series([1,3,5,7,9])
defaultSeries
Out[2]:
0    1
1    3
2    5
3    7
4    9
dtype: int64
In [5]:
defaultSeries[2]
Out[5]:
5
In [9]:
seriesCustomIndex = Series([1,3,5], index = [ 'one','three', 'five'])
In [10]:
seriesCustomIndex
Out[10]:
one      1
three    3
five     5
dtype: int64
In [11]:
seriesCustomIndex['three']
Out[11]:
3
In [13]:
data = {
        'name':['purna','ramu','darshan'] ,
        'age':[37,37,37],
        'location':['vvn','kphb','kphb']
        }

from pandas import DataFrame
frame = DataFrame(data)
In [14]:
frame
Out[14]:
name age location
0 purna 37 vvn
1 ramu 37 kphb
2 darshan 37 kphb
In [15]:
new_frame = DataFrame(frame,columns=['name','location','age'])

new_frame
Out[15]:
name location age
0 purna vvn 37
1 ramu kphb 37
2 darshan kphb 37
In [16]:
new_frame('age')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-7148ce2a833a> in <module>
----> 1 new_frame('age')

TypeError: 'DataFrame' object is not callable
In [17]:
new_frame['age']
Out[17]:
0    37
1    37
2    37
Name: age, dtype: int64
In [18]:
 
C:\ProgramData\Anaconda34\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.
Out[18]:
name        darshan
location       kphb
age              37
Name: 2, dtype: object
In [19]:
new_frame.iloc[2]
Out[19]:
name        darshan
location       kphb
age              37
Name: 2, dtype: object
In [20]:
new_frame['Education']='MS'
In [21]:
new_frame
Out[21]:
name location age Education
0 purna vvn 37 MS
1 ramu kphb 37 MS
2 darshan kphb 37 MS
In [22]:
new_frame.T
Out[22]:
0 1 2
name purna ramu darshan
location vvn kphb kphb
age 37 37 37
Education MS MS MS
In [23]:
new_frame.T
Out[23]:
0 1 2
name purna ramu darshan
location vvn kphb kphb
age 37 37 37
Education MS MS MS
In [24]:
new_frame.T
Out[24]:
0 1 2
name purna ramu darshan
location vvn kphb kphb
age 37 37 37
Education MS MS MS
In [25]:
new_frame
Out[25]:
name location age Education
0 purna vvn 37 MS
1 ramu kphb 37 MS
2 darshan kphb 37 MS
In [31]:
new_frame.drop('location',axis=1)
Out[31]:
name age Education
0 purna 37 MS
1 ramu 37 MS
2 darshan 37 MS
In [34]:
new_frame.drop(1)
Out[34]:
name location age Education
0 purna vvn 37 MS
2 darshan kphb 37 MS
In [ ]:
 

No comments:

Post a Comment

Search 24 Bytes