Python

How to Drop Columns from Pandas DataFrame

Here are two approaches to drop columns from Pandas DataFrame (1) Drop a single column from the DataFrame: df = df.drop(‘column name’,axis=1) (2) Drop multiple columns from the DataFrame: df = df.drop([‘column 1′,’column 2′,’column 3′,…],axis=1) In the next section, you’ll see how to apply the above two approaches using a simple example. The Example To …

How to Drop Columns from Pandas DataFrame Read More »

How to Randomly Select Columns from Pandas DataFrame

Depending on your needs, you may use either of the 4 techniques below in order to randomly select columns from Pandas DataFrame: (1) Randomly select a single column: df = df.sample(axis=’columns’) (2) Randomly select a specified number of columns. For example, to select 3 random columns, set n=3: df = df.sample(n=3,axis=’columns’)  (3) Allow a random …

How to Randomly Select Columns from Pandas DataFrame Read More »

How to Convert Strings to Integers in a Python List

You may apply either of the two methods below in order to convert strings to integers in a Python list: (1) Using map mylist = [‘value1′,’value2′,’value3’,…] mylist = list(map(int, mylist)) (2) Using list comprehension mylist = [‘value1′,’value2′,’value3′,…] mylist = [int(i) for i in mylist] Let’s now review several examples. Using Map to Convert Strings to …

How to Convert Strings to Integers in a Python List Read More »

How to Concatenate Two Lists in Python (with examples)

You can use either of the following templates in order to concatenate two lists in Python: (1) Using the + operator: list_one = [‘item1′,’item2′,’item3’,….] list_two = [‘item1′,’item2′,’item3’,….] concatenated_list = list_one + list_two print (concatenated_list) (2) Using extend: list_one = [‘item1′,’item2′,’item3’,….] list_two = [‘item1′,’item2′,’item3′,….] list_one.extend(list_two) print (list_one) In the next section, you’ll see how to apply …

How to Concatenate Two Lists in Python (with examples) Read More »