<jupyter_start><jupyter_text>PANDAS  Pandas Series  --->>Built for economic and financial work  --->>Written for data manipulation and data analysis<jupyter_code>"""
create pandas series
"""
import pandas as pd
pd.Series([1,2,3,4,5])
series = pd.Series([1,2,3,4,5])
type(series)
series.axes
series.dtype
series.size
series.ndim
series.values
series.head
series.head(3)
series.tail
series.tail(2)
#index naming
pd.Series([99,22,332,94,5], index=[1,5,9,4,3])
pd.Series([99,22,332,94,5], index=["a","b","c","d","e"])
#create list with dictionary
dictionary = pd.Series({"reg":10, "log":11, "cart":12})
series = pd.Series(dictionary)
series
# combining two series
pd.concat([series,series])
# element query
"reg" in series
"aa" in series<jupyter_output><empty_output><jupyter_text>Pandas DataFrame --->>is pandas data structure<jupyter_code>#create pandas dataframe
l = [1,2,39,67,90]
pd.DataFrame(l,columns=["degisken_ismi"])
import numpy as np
np.arange(1,10).reshape([3,3])
pd.DataFrame(m,columns=["data1","data2","data3"])
# df naming
df = pd.DataFrame(m,columns=["var1","var2","var3"])
df.head()
df.columns = ("dg1","dg2","dg3")
df
"""
Element operations
"""
s1 = np.random.randint(10,size=5)
s2 = np.random.randint(10,size=5)
s3 = np.random.randint(10,size=5)
dictionary = {"var1":s1,"var2":s2,"var3":s3}
dictionary
df = pd.DataFrame(dictionary)
df
df[0:1]
df.index
df.index = ["a","b","c","d","e"]
df
df["c":"e"]
# deletion
df.drop ("a",axis=0)
# temporary delete
df.drop("b",axis=0, inplace=True)
df
#permanently delete
#fancy
l = ["c","e"]
df.drop(l,axis=0)
df
# for variable
"var1" in df
l = ["var1","var2","var3"]
for i in l:
    print(i in df)
df["var4"] = df["var1"] / df["var2"]
df
# deletion variable
df.drop("var4",axis=1)
df
"""
loc && iloc
"""
m = np.random.randint(1,30,size=(10,3))
df=pd.DataFrame(m,columns=["var1","var2","var3"])
df
df.loc[0:3]
df.iloc[0:3]
df.iloc[0,0]
df.iloc[:3,:2]
df.loc[0:3,"var3"]
df.iloc[0:3,"var3"]   # errors do not use for "var3"
df.iloc[0:3]["var3"]
"""
conditional element operations
"""
m = np.random.randint(1,30,size=(10,3))
df = pd.DataFrame(m,columns=["var1","var2","var3"])
df
df["var1"]
df["var1"][0:2]
df[0:2]
df[0:2]["var1","var2"]    # errorss
df[0:2][["var1","var2"]]
df.var1
df[df.var1 > 10]["var1"]
df[df.var1 > 10]
df[(df.var1 > 1) & (df.var3 < 10)]
df[(df.var1 > 15), ["var1","var2"]]    # error
df.loc[(df.var1 > 15), ["var1","var2"]] 
df[(df.var1 > 15)][["var2","var3"]]
"""
merge operations
"""
m = np.random.randint(1,30,size=(5,3))
df1=pd.DataFrame(m,columns=["var1","var2","var3"])
df1
df2 = df1 + 99
df2
pd.concat([df1,df2])
pd.concat([df1,df2],ignore_index=True)
df1.columns
df2.columns
df2.columns = ["var1","var2","deg3"]
df1
df2
pd.concat([df1,df2])
pd.concat([df1,df2],join="inner")
df3 = pd.concat([df1, df2], axis=1)
df3 = df3.reindex(df1.index)
df3
df4 = pd.concat([df1, df2], axis=1)
df4 = df3.reindex(df2.index)
df4
"""
Advanced join operations
"""
# one-to-one merge
df1 = pd.DataFrame({'employees':['Ali','Veli','Ayse','Fatma'],
                   'group':['account','engineer','engineer','HR']})
df1
df2 = pd.DataFrame({'employees':['Ali','Veli','Ayse','Fatma'],
                   'startDate':[2010,2009,2014,2019]})
df2
pd.merge(df1,df2)
pd.merge(df1,df2,on='employees')
# many-to-one merge
df3 = pd.merge(df1,df2)
df3
df4 = pd.DataFrame({'group':['account','engineer','HR'],
                   'director':['caner','mustafa','berkcan']})
df4
pd.merge(df3,df4)
# many-to-many merge
df5=pd.DataFrame({'group':['account','account','engineer','engineer','HR','HR'],
                          'ability':['math','excel','Code','linux','excel','manager']})
df5
df1
pd.merge(df1,df5)<jupyter_output><empty_output><jupyter_text>Grouping & Aggregation Operation  Simple aggregation operations"""---count()---first()---last()---mean()---median()---min()---max()---std()---var()---sum()"""<jupyter_code>import seaborn as sns
df = sns.load_dataset("planets")
df.head()
df.shape
df.mean()
df["mass"].mean()
df["mass"].count()
df["mass"].min()
df["mass"].max()
df["mass"].sum()
df["mass"].std()    #standard deviation
df["mass"].var()    # variance
df.describe()
df.describe().T
df.dropna().describe().T
"""
Grouping
"""
df = pd.DataFrame({'group':['A','B','C','A','B','C'],
                  'data':[10,11,52,23,43,55]},columns=['group','data'])
df
df.groupby("group")
df.groupby("group").mean()
df.groupby("group").sum()
df = sns.load_dataset("planets")
df.head()
df.groupby("method")
df.groupby("method")["orbital_period"]
df.groupby("method")["orbital_period"].mean()
df.groupby("method")["mass"].mean()
df.groupby("method")["mass"].describe()
"""
Aggregate
"""
df = pd.DataFrame({'group':['A','B','C','A','B','C'],
                  'var1':[10,23,33,22,11,99],
                  'var2':[100,253,333,262,111,969]},
                 columns=['group','var1','var2'])
df
df.groupby("group")
df.groupby("group").mean()
df.groupby("group").aggregate(["min",np.median,max])
df.groupby("group").aggregate({"var1":min,"var2":max})
"""
filter
"""
def filter_func(x):
    return x["var1"].std()>9
df.groupby("group").filter(filter_func)
df.groupby("group").std()
"""
transfrom
"""
df
df['var1']*9
df.transform(lambda x: x-x.mean())
df[:,1:2]           # error
df.iloc[:,1:2]
df_a.transform(lambda x:x-x.mean())
df_a.transform(lambda x: x-x.mean()) / std()
"""
apply
"""
df.apply(np.sum)
df.apply(np.mean)
df.groupby("group").apply(np.sum)
df.groupby("group").apply(np.mean)
"""
Pivot tables
"""
~~groupby
titanic = sns.load_dataset('titanic')
titanic.head()
titanic.groupby("sex")["survived"].mean()
titanic.groupby("sex")[["survived"]].mean()
titanic.groupby(["sex","class"])[["survived"]].aggregate("mean").unstack()
titanic.groupby(["sex","class"])[["survived"]].aggregate("mean")
# pivot
titanic.pivot_table("survived",index="sex",columns="class")
"""
Read external data
"""
#reading csv
pd.read.csv("reading_data/example.csv")
pd.read.csv("reading_data/example.csv",sep=";")
#reading txt
pd.read_csv("reading_data/sampletxt.txt")
pd.read_excel("reading_data/sampletxt.xlsv")
df = pd.read_excel("reading_data/sampletxt.xlsv")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2528172464478197, 'fraction_numerical': 0.0514453699167075, 'mean_word_length': 6.006864988558353, 'pattern_counts': {'":': 9, '<': 14, '<?xml version=': 0, '>': 26, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '659008', 'license': 'MIT', 'n_tokens_mistral': 2656, 'n_tokens_neox': 2528, 'n_words': 368, 'path': 'Pandas.ipynb', 'repo_name': 'meminds/Data_Manipulation', 'size': '6123'}