Mindmap
Python 3
Inhalt der Mindmap
Python 3
Print
- print("string")
- print(variable)
- print first letter = print(ListVariable[0])
- print last letter = print(ListVariable[-1])
- print first letter = print(ListVariable[0])
- print(variable + "string") - called concatenate
- print("Captain {} is {} years old.".format(name, age))
- print("""Multi-line text""")
Syntax
- "string"
- """Multi-line text"""
- combined_string = ("Captain {} is {} years old.".format(name, age))
- combined_string_with_var_to_two_decimal_places = ("This items costs: {:.2f}".format(float)
- Example_string = 'this_is_a_string'
- newList = Example_string.split('_')
- newList = ['this', 'is', 'a', 'string']
- newString = '-'.join(newList)
- newString = 'this-is-a-string'
- newString = '-'.join(newList)
- Note: to seperate by tab key '\t' or new line '\n'
- newList = ['this', 'is', 'a', 'string']
- Example_string.strip('_')
- removes '_'
- Example_string.replace('_', ' ')
- replaces first arg with second
- Example_string.find('i')
- returns list with indices of arg
- newList = Example_string.split('_')
- int(1)
- float(0.2)
- round(float, No. of decimals)
- "string"
def function(arg1, arg2):
- return responseA, responseB
- to set a default value: arg2=global_variable_name
Control Flow
- If (not) condition 1 and/or (not) condition 2:
- elif (not) condition 3
- else: (not) condition 4
- elif (not) condition 3
- try: function()
- except Type-of-error:
- print "You raised an error" (for example)
- except Type-of-error:
- for temp_variable in list:
- if temp_variable == a condition that means it's not wanted:
- continue
- if temp_variable == a condition which can stop the search
- break
- if temp_variable == a condition that means it's not wanted:
- If (not) condition 1 and/or (not) condition 2:
Lists
- list = [data]
- list = [[sub-list], [sub-list2]]
- listA_and_B = zip(listA, listB)
- - Note this will create a zip object not a list
- listA_and_B = list(ListA_and_B)
- - Note this will create a zip object not a list
- list.append(data)
- list2 = list1 + [data1, data2]
- list = [data1, data2, data3]
- newList = [var for var in list if var (condition)]
- len(list)
- list[0] (first) list[-1] (last)
- list[0:2] (first 3)
- list[2:4] (third and fourth)
- list[-2:] (last two items)
- list[2:4] (third and fourth)
- list[0:2] (first 3)
- list.count("Character") - returns int
- list.sort() - sorts original list, doesn't return anything
- newList = sorted(list) - returns a new list
- list = list[1:] - removes first datum
- list = list[:-1] - removes last datum
- list = [data]
Tuples
- my_tuple = (Data1, Data2, Data3)
- Works like a list to store multiple data types
- can print elements with my_tuple[x]
- Can NOT overwrite data (inmutable)
- can print elements with my_tuple[x]
- Var1, Var2, Var3 = my_tuple
- Works like a list to store multiple data types
- my_tuple = (Data1, Data2, Data3)
PANDAS / CSV
- import pandas as pd
- import csv
- Data is put in a dataframe
- var = pd.read_csv('NameOfFile.csv')
- var = pd.read_xlsx('NameOfFile.xlsx')
- excel file
- var = pd.read_csv('NameOfFile.txt')
- USING THE DATA
- print(dfvar.head(number_of_rows))
- prints top rows
- print(dfvar.tail(number_of_rows))
- prints bottom rows
- print(dfvar.columns)
- prints a list type format
- print(dfvar.columns[i])
- prints column at index i
- number of columns = len(dfvar.columns)
- print all in column - print(dfvar[Name_of_column])
- multiple columns, use a list - print(dfvar[[column1, column2]]
- iloc = index location function
- print rows X to Y - print(dfvar.iloc[rowX: rowY)
- print specific data point X,Y - print(dfvar.iloc[X, Y])
- iterrows = iterate through rows
- i.e. for every index, return data in all rows
- for index, row in dfvar.iterrows():
- print(index, row)
- to only print data from certain columns;-
- print(index, row[column1: etc])
- to only print data from certain columns;-
- print(index, row)
- iterating through index for data with conditions
- print rows that meed condition using .loc ;-
- print(dfvar.loc[dfvar['Column Name'] == 'Condition']
- print rows that meed condition using .loc ;-
- dfvar.describe
- gives you information such as standard deviation & mean etc.
- Sorting
- dfvar.sort_values('Column_name')
- sorts alphabetically
- dfvar.sort_values(['Column 1', 'Column 2'], ascending = [1,0])
- ascending = 1 means true, i.e alphebetical or 0 - 100
- 0 = false i.e. reverse
- ascending = 1 means true, i.e alphebetical or 0 - 100
- dfvar.sort_values('Column_name')
- EDITING THE DATA
- Adding Columns
- dfvar['New_Column_Name'] = dfvar['Column1'] + dfvar['Column2']
- or
- dfvar['New_Column_Name'] = df.iloc[XColumn: YColumn, XRow:YRow].sum(axis = 1)
- note df.iloc[] locates the data within those ranges
- .sum(axis = 1) specifies adding them all together horizontally
- axis = 0 would be vertically
- .sum(axis = 1) specifies adding them all together horizontally
- To move the position of the new column(s);-
- newList = list(dfvar.columns.values)
- dfvar = dfvar[newList[X:Y] + [newList[-1]] + newList[Y+1:Z]]
- The range X:Y is the columns you want before
- newList[-1] is the final column that you have added
- newList[Y+1:Z] is the remaining columns to go on the end
- Note: newList[-1] is one column and will be considered a string, hence the extra square brackets
- newList[-1] is the final column that you have added
- The range X:Y is the columns you want before
- dfvar = dfvar[newList[X:Y] + [newList[-1]] + newList[Y+1:Z]]
- newList = list(dfvar.columns.values)
- note df.iloc[] locates the data within those ranges
- dfvar['New_Column_Name'] = df.iloc[XColumn: YColumn, XRow:YRow].sum(axis = 1)
- or
- dfvar['New_Column_Name'] = dfvar['Column1'] + dfvar['Column2']
- Dropping a column
- dfvar = dfvar.drop(columns=['Column_name_to_be_removed']
- EXPORTING THE DATA
- dfvar.to_csv('filename.csv', index=False)
- Index=False prevents adding the index numbers as the left most column
- dfvar.to_excel('filename.xlsx', index=False)
- dfvar.to_csv('filename.txt', index=False, sep = '\t')
- sep='\t' means they will be seperated by a tab key
- dfvar.to_csv('filename.txt', index=False, sep = '\t')
- dfvar.to_csv('filename.csv', index=False)
- Adding Columns
- EDITING THE DATA
- print(dfvar.head(number_of_rows))
- text file - note data is imported to single column
- to import to tab seperated;-
- var = pd.read_csv('NameOfFile.txt', delimiter=\t)
- where the '\t' denotes the tab key
- var = pd.read_csv('NameOfFile.txt', delimiter=\t)
- to import to tab seperated;-
- USING THE DATA
- comma seperated values file
- var = pd.read_xlsx('NameOfFile.xlsx')
- length of data = len(var)
- var = pd.read_csv('NameOfFile.csv')
- Data is put in a dataframe
- import csv
- import pandas as pd
Dictionairys
- my_dict = {}
- my_dict["new key"] = value
- my_dict.update({key1: value1, key2: value2])
- Combing two lists into a dictionairy
- new_dict = {key:value for key, value in zip(list1, list2)}
- my_dict = {}
CLASSES
- class ClassName:
Informationen
- Beschreibung
- Not an exam Computer Studies Mindmap am Python 3, erstellt von Mike Peterson am 10/05/2020.
- Erstellt von
- Mike Peterson
- Aufrufe
- 4
- Tags
- Bewertung