Pandas Session 7 Code

Pandas Session 7 Code For Video Click Fahad Hussain CS


Hi, Welcome to Fahad Hussain CS blog, do not forget to subscribe and follow the blog and YouTube channel  Fahad Hussain CS

Code:

## Hierarchical Indexing

"""

import numpy as np

import pandas as pd

data = pd.Series(np.random.randn(9),

                 index=[['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd'],

                        [1, 2, 3, 1, 3, 1, 2, 2, 3]])

print(data)


data.index


print(data['b'])

print(data['b':'c'])

print(data.loc[['b', 'd']])


data.loc[:, 2]


print(data)

print(data.unstack())


data.unstack().stack()


frame = pd.DataFrame(np.arange(12).reshape((4, 3)),

                     index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]],

                     columns=[['Ohio', 'Ohio', 'Colorado'],

                              ['Green', 'Red', 'Green']])

print(frame)


frame.index.names = ['key1', 'key2']

frame.columns.names = ['state', 'color']

print(frame)


frame['Ohio']


"""MultiIndex.from_arrays([['Ohio', 'Ohio', 'Colorado'], ['Green', 'Red', 'Green']],

                       names=['state', 'color'])


```

# This is formatted as code

```


### Reordering and Sorting Levels

"""


frame.swaplevel('key1', 'key2')


frame.sort_index(level=1)

frame.swaplevel(0, 1).sort_index(level=0)


"""### Summary Statistics by Level"""


print(frame.sum(level='key2'))

print(frame.sum(level='color', axis=1))


"""


```

# This is formatted as code

```


### Indexing with a DataFrame's columns

"""


frame = pd.DataFrame({'a': range(7), 'b': range(7, 0, -1),

                      'c': ['one', 'one', 'one', 'two', 'two',

                            'two', 'two'],

                      'd': [0, 1, 2, 0, 1, 2, 3]})

frame


frame2 = frame.set_index(['c', 'd'])

frame2


frame.set_index(['c', 'd'], drop=False)


frame2.reset_index()


"""```

# This is formatted as code

```


## Combining and Merging Datasets


### Database-Style DataFrame Joins

"""


df1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],

                    'data1': range(7)})

df2 = pd.DataFrame({'key': ['a', 'b', 'd'],

                    'data2': range(3)})

df1

df2


pd.merge(df1, df2)


pd.merge(df1, df2, on='key')


df3 = pd.DataFrame({'lkey': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],

                    'data1': range(7)})

df4 = pd.DataFrame({'rkey': ['a', 'b', 'd'],

                    'data2': range(3)})

pd.merge(df3, df4, left_on='lkey', right_on='rkey')


pd.merge(df1, df2, how='outer')


df1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'b'],

                    'data1': range(6)})

df2 = pd.DataFrame({'key': ['a', 'b', 'a', 'b', 'd'],

                    'data2': range(5)})

df1

df2

pd.merge(df1, df2, on='key', how='left')


pd.merge(df1, df2, how='inner')


left = pd.DataFrame({'key1': ['foo', 'foo', 'bar'],

                     'key2': ['one', 'two', 'one'],

                     'lval': [1, 2, 3]})

right = pd.DataFrame({'key1': ['foo', 'foo', 'bar', 'bar'],

                      'key2': ['one', 'one', 'one', 'two'],

                      'rval': [4, 5, 6, 7]})

pd.merge(left, right, on=['key1', 'key2'], how='outer')


print(pd.merge(left, right, on='key1'))

print(pd.merge(left, right, on='key1', suffixes=('_left', '_right')))


"""### Merging on Index"""


left1 = pd.DataFrame({'key': ['a', 'b', 'a', 'a', 'b', 'c'],

                      'value': range(6)})

right1 = pd.DataFrame({'group_val': [3.5, 7]}, index=['a', 'b'])

left1

right1

pd.merge(left1, right1, left_on='key', right_index=True)


pd.merge(left1, right1, left_on='key', right_index=True, how='outer')


lefth = pd.DataFrame({'key1': ['Ohio', 'Ohio', 'Ohio',

                               'Nevada', 'Nevada'],

                      'key2': [2000, 2001, 2002, 2001, 2002],

                      'data': np.arange(5.)})

righth = pd.DataFrame(np.arange(12).reshape((6, 2)),

                      index=[['Nevada', 'Nevada', 'Ohio', 'Ohio',

                              'Ohio', 'Ohio'],

                             [2001, 2000, 2000, 2000, 2001, 2002]],

                      columns=['event1', 'event2'])

lefth

righth


pd.merge(lefth, righth, left_on=['key1', 'key2'], right_index=True)

pd.merge(lefth, righth, left_on=['key1', 'key2'],

         right_index=True, how='outer')


left2 = pd.DataFrame([[1., 2.], [3., 4.], [5., 6.]],

                     index=['a', 'c', 'e'],

                     columns=['Ohio', 'Nevada'])

right2 = pd.DataFrame([[7., 8.], [9., 10.], [11., 12.], [13, 14]],

                      index=['b', 'c', 'd', 'e'],

                      columns=['Missouri', 'Alabama'])

left2

right2

pd.merge(left2, right2, how='outer', left_index=True, right_index=True)


left2.join(right2, how='outer')


left1.join(right1, on='key')


another = pd.DataFrame([[7., 8.], [9., 10.], [11., 12.], [16., 17.]],

                       index=['a', 'c', 'e', 'f'],

                       columns=['New York', 'Oregon'])

another

left2.join([right2, another])

left2.join([right2, another], how='outer')


No comments:

Post a Comment

Fell free to write your query in comment. Your Comments will be fully encouraged.