Basics Link to heading
Create DataFrame
df = pd.DataFrame([[1,0],[4,5]], columns=['A','B'])
df = df.append(pd.DataFrame([[6,9]], columns=list(df.columns)))
#
df = pd.read_csv("File.csv")
#
df.shape
df.info()
df.head()
df.tail()
Get all row with one column. This returns Series
df["col1"]
df["col1"].value_counts()
return multiple columns. This returns Dataframe
df[["col1","col2"]]
This returns pandas.core.indexes.base.Index.
df.columns
list(df.columns)
#pandas.core.series.Series
df.iloc[0]
df.iloc[0]["col1"]
df.iloc[ [0,1] ]
df.iloc[ [0,1] , [0,1] ]
df.loc[ [0,1] , ["col1","col2"] ]
df.loc[ 0:1 , "col1":"col2" ]
df.index
df.set_index('col1',inplace=True)
df.reset_index(inplace=True)
#
df = pd.read_csv("File.csv",index_col = "col")
#
df.sort_index(inplace=True)
Filtering Link to heading
filt = df["col"] == value
filt = (df["col"] == value | df["col1"] == value)
#
df[filt]
#
df.loc(filt)
# negation
df.loc(~filt)
#
filt = df["col"].isin(["v1","v2"])
# na=False ignore Nan
filt = df["col"].str.containts(["v1","v2"],na=False)
Update rows/columns Link to heading
# change columns names
df.columns = [x.lower() for x in df.columns]
df.rename(columns={"col":"col_new"}, incplace = True)
# change all values of a row
df.loc[0] = ["v1","v2"]
# change one column
df.loc[0,"col"] = "new_value"
df.loc[0,["col"]] = ["new_value"]
# change all rows
df["col"] = ['v1','v2']
Operation to update row/colums Link to heading
operations on series using .apply
def func(x):
return ""
df["col"].apply(func)
operations on dataFrame using .applymap
df.apply(len)
Change value in column series
df["col"].map({"v1":"nv1"})
df["col"].replease({"v1":"nv1"})
Added/delete rows columns Link to heading
df["newcol"] = df["col"] + 1
df.drop(columsn=["col"], inplace = True)
df = df.append({"A":10}, ignore_index = True)
df.drop(index=4)
df.drop(index=df[df["A"]==1].index)
sort Link to heading
df.sort_values(by="A", ascending=False,inplace=True )
df["A"].sort_values()