Skip to main content

Pandas Operations - Unique, Value Counts, Apply, Drop, Columns, Index, Sort Values, Is Null, Pivot Tables - Python

Pandas Operations - Unique, Nunique, Value Counts, Apply, Drop, Columns, Index, Sort Values, Is Null, Pivot Tables
Run the python code here: https://repl.it/@VinitKhandelwal/pandas-operations
import numpy as np
import pandas as pd

df = pd.DataFrame({'col1':[1,2,3,4],'col2':[1111,2222,3333,2222],'col3':['aaa','bbb','ccc','ddd']})
print(df)
print("LIST OF UNIQUE VALUES IN A COLUMN")
print(df['col2'].unique())
print("COUNT OF UNIQUE VALUES IN A COLUMN")
print(df['col2'].nunique())
print("COUNT OF VALUES IN A COLUMN")
print(df['col2'].value_counts())
print("CONDITIONAL SELECTION")
print(df[(df['col1']>2) & (df['col2']<3333)])
print(df['col1']>2)
print("APPLY")
def times2(x):
return x*2
print(df.apply(times2))
print(df['col3'].apply(len))
print("APPLY LAMBDA FUNCTION")
print(df.apply(lambda x: x*3))
print("DROP")
print(df.drop('col1', axis=1))
df.drop('col1', axis=1, inplace=True)
print(df)
df['col1']=[1,2,3,4]
print(df)
print("DEATILS OF COLUMNS AND INDICES")
print(df.columns)
print(df.index)
print("SORT COLUMN")
print(df.sort_values('col2'))
print("IS NULL")
print(df.isnull())
print("PIVOT TABLE")
df = pd.DataFrame({'A':['foo', 'foo', 'foo', 'bar', 'bar', 'bar'], 'B':['one', 'one', 'two', 'two', 'one', 'one'], 'C':['x', 'y', 'x', 'y', 'x', 'y'], 'D':[1,3,2,5,4,1]})
print(df)
df2 = df.pivot_table(values='D', index=['A','B'], columns=['C'])
print(df2)
print("DEATILS OF COLUMNS AND INDICES")
print(df2.columns)
print(df2.index)

OUTPUT

   col1  col2 col3
0     1  1111  aaa
1     2  2222  bbb
2     3  3333  ccc
3     4  2222  ddd
LIST OF UNIQUE VALUES IN A COLUMN
[1111 2222 3333]
COUNT OF UNIQUE VALUES IN A COLUMN
3
COUNT OF VALUES IN A COLUMN
2222    2
1111    1
3333    1
Name: col2, dtype: int64
CONDITIONAL SELECTION
   col1  col2 col3
3     4  2222  ddd
0    False
1    False
2     True
3     True
Name: col1, dtype: bool
APPLY
   col1  col2    col3
0     2  2222  aaaaaa
1     4  4444  bbbbbb
2     6  6666  cccccc
3     8  4444  dddddd
0    3
1    3
2    3
3    3
Name: col3, dtype: int64
APPLY LAMBDA FUNCTION
   col1  col2       col3
0     3  3333  aaaaaaaaa
1     6  6666  bbbbbbbbb
2     9  9999  ccccccccc
3    12  6666  ddddddddd
DROP
   col2 col3
0  1111  aaa
1  2222  bbb
2  3333  ccc
3  2222  ddd
   col2 col3
0  1111  aaa
1  2222  bbb
2  3333  ccc
3  2222  ddd
   col2 col3  col1
0  1111  aaa     1
1  2222  bbb     2
2  3333  ccc     3
3  2222  ddd     4
DEATILS OF COLUMNS AND INDICES
Index(['col2', 'col3', 'col1'], dtype='object')
RangeIndex(start=0, stop=4, step=1)
SORT COLUMN
   col2 col3  col1
0  1111  aaa     1
1  2222  bbb     2
3  2222  ddd     4
2  3333  ccc     3
IS NULL
    col2   col3   col1
0  False  False  False
1  False  False  False
2  False  False  False
3  False  False  False
PIVOT TABLE
     A    B  C  D
0  foo  one  x  1
1  foo  one  y  3
2  foo  two  x  2
3  bar  two  y  5
4  bar  one  x  4
5  bar  one  y  1
C          x    y
A   B
bar one  4.0  1.0
    two  NaN  5.0
foo one  1.0  3.0
    two  2.0  NaN
DEATILS OF COLUMNS AND INDICES
Index(['x', 'y'], dtype='object', name='C')
MultiIndex(levels=[['bar', 'foo'], ['one', 'two']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
           names=['A', 'B'])

Comments

Popular posts from this blog

Python - List - Append, Count, Extend, Index, Insert, Pop, Remove, Reverse, Sort

🐍 Advance List List is widely used and it's functionalities are heavily useful. Append Adds one element at the end of the list. Syntax list1.append(value) Input l1 = [1, 2, 3] l1.append(4) l1 Output [1, 2, 3, 4] append can be used to add any datatype in a list. It can even add list inside list. Caution: Append does not return anything. It just appends the list. Count .count(value) counts the number of occurrences of an element in the list. Syntax list1.count(value) Input l1 = [1, 2, 3, 4, 3] l1.count(3) Output 2 It returns 0 if the value is not found in the list. Extend .count(value) counts the number of occurrences of an element in the list. Syntax list1.extend(list) Input l1 = [1, 2, 3] l1.extend([4, 5]) Output [1, 2, 3, 4, 5] If we use append, entire list will be added to the first list like one element. Extend, i nstead of considering a list as one element, it joins the two lists one after other. Append works in the following way. Input l1 = [1, 2, 3] l1.append([4, 5]) Output...

Difference between .exec() and .execPopulate() in Mongoose?

Here I answer what is the difference between .exec() and .execPopulate() in Mongoose? .exec() is used with a query while .execPopulate() is used with a document Syntax for .exec() is as follows: Model.query() . populate ( 'field' ) . exec () // returns promise . then ( function ( document ) { console . log ( document ); }); Syntax for .execPopulate() is as follows: fetchedDocument . populate ( 'field' ) . execPopulate () // returns promise . then ( function ( document ) { console . log ( document ); }); When working with individual document use .execPopulate(), for model query use .exec(). Both returns a promise. One can do without .exec() or .execPopulate() but then has to pass a callback in populate.

683 K Empty Slots

  Approach #1: Insert Into Sorted Structure [Accepted] Intuition Let's add flowers in the order they bloom. When each flower blooms, we check it's neighbors to see if they can satisfy the condition with the current flower. Algorithm We'll maintain  active , a sorted data structure containing every flower that has currently bloomed. When we add a flower to  active , we should check it's lower and higher neighbors. If some neighbor satisfies the condition, we know the condition occurred first on this day. Complexity Analysis Time Complexity (Java):  O(N \log N) O ( N lo g N ) , where  N N  is the length of  flowers . Every insertion and search is  O(\log N) O ( lo g N ) . Time Complexity (Python):  O(N^2) O ( N 2 ) . As above, except  list.insert  is  O(N) O ( N ) . Space Complexity:  O(N) O ( N ) , the size of  active . Approach #2: Min Queue [Accepted] Intuition For each contiguous block ("window") of  k  po...