DEV Community

sandeepsamanth
sandeepsamanth

Posted on

DAY-26-PANDAS

import pandas as pd 
data = {"a":[1,2,3,4],
       "b":[4,5,6,7],
       "c":["sudh" , "krish","hitesh","navin"]}
df= pd.DataFrame(data)
df.set_index('a',inplace=True)
df = df.reset_index()

Enter fullscreen mode Exit fullscreen mode
data = {"a":[1,2,3,4],
       "b":[4,5,6,7],
       "c":["sudh" , "krish","hitesh","navin"]}
df1 = pd.DataFrame(data,index = ['a','b','c','d'])
df1.reindex(['b','c','d','a'])

for i,j in df1.iterrows():
    print( j)

for col_name , column in df1.iteritems():
    print( col_name , column)

def test(x):
    return x.sum()
df1.apply(test,axis=0)
//a                      10
//b                      22
//c    sudhkrishhiteshnavin

df2 = df1[['a','b']]
df2.applymap(lambda x : x**2)
df.sort_values('c')
df.sort_index(ascending = False)

Enter fullscreen mode Exit fullscreen mode
#Python Pandas - Window Functions
df4 = pd.DataFrame({'a' : [3,4,5,2,1,3,4,5,6]})
df4['a'].rolling(window=1).mean()
df4['a'].rolling(window=3).mean()

Enter fullscreen mode Exit fullscreen mode
#Python Pandas - Date Functionality

date = pd.date_range(start='2023-04-23' , end = '2023-06-23')
df_date = pd.DataFrame({'date':date})
df7 = pd.DataFrame({"date" : ['2023-06-23' , '2023-06-22','2023-06-20']})
df7['updated_date'] = pd.to_datetime(df7['date'])
df7['year'] = df7['updated_date'].dt.year
Enter fullscreen mode Exit fullscreen mode
#Python Pandas –Time Delta
pd.Timedelta(days= 1,hours = 5 ,minutes = 45)
dt = pd.to_datetime('2023-06-20')
td = pd.Timedelta(days = 1 )
dt+td
Timestamp('2023-06-21 00:00:00')

Enter fullscreen mode Exit fullscreen mode
#Python Pandas - Categorical Data

data = ["sudh" , "krish" , "hitesh" , "navin","sudh" ,"sudh" ]
cat = pd.Categorical(data)
cat.value_counts()

Enter fullscreen mode Exit fullscreen mode
#Python Pandas – Visualization
d = pd.Series([1,2,3,3,5,6,6,8])
d
d.plot()
df = pd.DataFrame({'a':[3,4,5,6,7],
                  'b':[4,5,6,7,8]})
df.plot(x= 'a',y='b')
df.plot.scatter(x= 'a',y='b')
d = pd.Series([1,2,3,3,5,6,6,8])
d
d.plot.pie()

Enter fullscreen mode Exit fullscreen mode

Top comments (0)