<jupyter_start><jupyter_text>Pandas series<jupyter_code>ser=pd.Series([22,34,76],["Deepak","Amit","Rashmi"])
ser
ser.index
ser.loc[['Deepak','Amit']]<jupyter_output><empty_output><jupyter_text>Series is 1D labelled array<jupyter_code>ser.loc['Deepak']
ser[[2]]
ser.iloc[0]
for i in (0:3):
    ser[[i]]
"Deepak" in ser
22 in ser
ser*2
ser**2
ser[['Amit','Deepak']]**2<jupyter_output><empty_output><jupyter_text>pandas DataFrameCreating DataFrame from dictionary of Python series<jupyter_code>d={'Age':pd.Series([22,23,45],index=['D','A','C']),
   'Salary':pd.Series([1000,2000,3000],index=['D','A','C'])}
df=pd.DataFrame(d)
df
df.index
df.columns
pd.DataFrame(d,index=['D','A'])
pd.DataFrame(d,index=['D','A','C'],columns=['Age'])
data=[{'Deepak':1,'Ashish':2},{'Kavita':5,'Anmole':10,"Sanyam":20,"Deepak":20}]
pd.DataFrame(data)
pd.DataFrame(data,index=["Section 1","Section 2"])
pd.DataFrame(data,columns=["Anmole","Sanyam"])<jupyter_output><empty_output><jupyter_text>Basic DataFrame Operations<jupyter_code>df
df["Age"]
df["Higher Salary"]=df["Salary"]>1000
df
Age=df.pop("Age")
Age
df
del df["Higher Salary"]
df
df.insert(0,'Copy of Salary',df['Salary'])
df
df.insert(1,'Copy of Salary 3',df['Salary'])<jupyter_output><empty_output><jupyter_text>Can duplicate values but col name should be unique<jupyter_code>df<jupyter_output><empty_output><jupyter_text>Case Study: Movie Data Analysis<jupyter_code>movies=pd.read_csv("movies.csv",sep=',')
print(type(movies))

movies.head(15)
movies.tail(10)<jupyter_output><empty_output><jupyter_text>Day 3 Coding<jupyter_code>tags=pd.read_csv("tags.csv",sep=",")
tags.head()
ratings=pd.read_csv("ratings.csv",sep=",",parse_dates=['timestamp'])
ratings.head()
del ratings["timestamp"]
del tags["timestamp"]
tags.head()

ratings.head()
#Extract 0th row: notice that it is infact a Series

row_0 = tags.iloc[0]
type(row_0)
row_0
row_0.index
row_0['userId']
row_0['movieId']
'rating' in row_0
for x in row_0:
    print (x)
row_0.name
row_0 = row_0.rename('first_row')
row_0.name
range(0,tags.size)
s=tags.size-1
s
for i in range(0,10,1):
    row=tags.iloc[i]
 
row<jupyter_output><empty_output><jupyter_text>DataFrames<jupyter_code>tags.head()
tags.index
tags.columns
# Extract row 0, 11, 2000 from DataFrame

tags.iloc[ [0,11,2000] ]
rows=tags.iloc[range(0,10,1)]
rows<jupyter_output><empty_output><jupyter_text>Descriptive statistics<jupyter_code>ratings['rating'].describe() #Only Rating wil be reflected
ratings.describe()
ratings['rating'].mean()
ratings.mean()
ratings['rating'].min()
ratings['rating'].max()
ratings['rating'].std()
ratings['rating'].mode()
ratings.corr()
filter_1 = ratings['rating'] > 5
print(filter_1)
filter_1.any()
filter_2 = ratings['rating'] > 0
print(filter_2)
filter_2.all()
type(filter_1)
print (all([False, True, False, False])) # if any 1 is false, all() returns false
print (any([False, True, False, False])) # if any 1 is true, any() returns true<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code>movies.shape
#is any row NULL ?

movies.isnull().any()
tags.isnull().any()
tags.shape<jupyter_output><empty_output><jupyter_text><jupyter_code>tags=tags.dropna() # never drop rows directly
tags.shape #no of rows and column
ratings.isnull().any()
tags.shape[0]

rows=tags.iloc[range(0,tags.shape[0],1)]
rows.head()<jupyter_output><empty_output><jupyter_text>No null values  Data visualisation<jupyter_code>%matplotlib inline

ratings.hist(column='rating', figsize=(15,10))
ratings.boxplot(column='rating', figsize=(15,10))<jupyter_output><empty_output><jupyter_text>Slicing out columns<jupyter_code>tags['tag'].head()
movies[['title','genres']].head()
ratings[-10:]
tag_counts = tags['tag'].value_counts()
tag_counts
%matplotlib inline
tag_counts[:10].plot(kind='bar', figsize=(15,10))<jupyter_output><empty_output><jupyter_text>Filters for Selecting Rows<jupyter_code>is_highly_rated = ratings['rating'] >= 4.0

ratings[is_highly_rated][30:50]
is_animation = movies['genres'].str.contains('Animation','IMAX')

movies[is_animation][5:15]
is_animation = movies['genres'].str.contains('Animation')

movies[is_animation][5:15]
movies[is_animation].head(15)<jupyter_output><empty_output><jupyter_text>Group By and Aggregate<jupyter_code>ratings_count = ratings[['movieId','rating']].groupby('rating').count()
ratings_count

ratings.shape
ratings_count.rename(columns = {"movieId": "No. of movies"}) 
ratings_count
average_rating = ratings[['movieId','rating']].groupby('movieId').mean()
average_rating.head()<jupyter_output><empty_output><jupyter_text>Merge Dataframes<jupyter_code>tags.head()
movies.head()
t = movies.merge(tags, on='movieId', how='inner')
t.head()<jupyter_output><empty_output><jupyter_text>Split 'genres' into multiple columns<jupyter_code>movie_genres = movies['genres'].str.split('|', expand=True)
movie_genres[:10]
movie_genres = movies['genres'].str.split('|', expand=False)
movie_genres[:10]<jupyter_output><empty_output><jupyter_text>Add a new column for comedy genre flag<jupyter_code>movie_genres['isComedy'] = movies['genres'].str.contains('Comedy')
movie_genres[:10]
movies['year'] = movies['title'].str.extract('.*\((.*)\).*', expand=True)
movies.tail()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18707940780619112, 'fraction_numerical': 0.028071524706787157, 'mean_word_length': 10.333333333333334, 'pattern_counts': {'":': 3, '<': 73, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '494490', 'license': 'Apache-2.0', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2037, 'n_words': 296, 'path': 'Python/Henry Harvin/Pandas Library.ipynb', 'repo_name': 'anmoledewan/PythonCodes', 'size': '5201'}