Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Server Side Programming Articles - Page 918 of 2650
804 Views
The Dataframe.loc is used to access a group of rows and columns by label or a boolean array. We will append a list to a DataFrame using loc. Let us first create a DataFrame. The data is in the form of lists of team rankings for our example −# data in the form of list of team rankings Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50], ['Bangladesh', 6, 40]] # Creating a DataFrame and adding columns dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])Following is the row to be ... Read More
2K+ Views
Mode is the value that appears the most in a set of values. Use the fillna() method and set the mode to fill missing columns with mode. At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a DataFrame with 2 columns. We have set the NaN values using the Numpy np.NaN −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'], "Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] } )Find mode of the column values with NaN i.e, for Units columns ... Read More
2K+ Views
We can search DataFrame for a specific value. Use iloc to fetch the required value and display the entire row. At first, import the required library −import pandas as pdCreate a DataFrame with 4 columns −dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 120, 150, 110, 200, 250] })Let’s search Car with Registeration Price 500 −for i in range(len(dataFrame.Car)): if 5000 == dataFrame.Reg_Price[i]: indx = iNow, display the found value −dataFrame.iloc[indx] ExampleFollowing is ... Read More
830 Views
The sort_index() is used to sort index in ascending and descending order. If you won’t mention any parameter, then index sorts in ascending order.At first, import the required library −import pandas as pdCreate a new DataFrame. It has unsorted indexes −dataFrame = pd.DataFrame([100, 150, 200, 250, 250, 500],index=[4, 8, 2, 9, 15, 11],columns=['Col1'])Sort the indexes −dataFrame.sort_index() ExampleFollowing is the code −import pandas as pd dataFrame = pd.DataFrame([100, 150, 200, 250, 250, 500],index=[4, 8, 2, 9, 15, 11],columns=['Col1']) print"DataFrame...",dataFrame print"Sort index...",dataFrame.sort_index()OutputThis will produce the following output −DataFrame... Col1 4 100 8 150 2 200 9 250 15 250 11 500 Sort index... Col1 2 200 4 100 8 150 9 250 11 500 15 250
326 Views
To add a prefix to all the column names, use the add_prefix() method. At first, import the required Pandas library −import pandas as pdCreate a DataFrame with 4 columns −dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 120, 150, 110, 200, 250] })Add a prefix to _column to every column using add_prefix() −dataFrame.add_prefix('column_') ExampleFollowing is the code −import pandas as pd # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": ... Read More
764 Views
To reverse the column order, use the dataframe.columns and set as -1 −dataFrame[dataFrame.columns[::-1]At first, import the required library −import pandas as pd Create a DataFrame with 4 columns −dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 120, 150, 110, 200, 250] })Reverse the column order −df = dataFrame[dataFrame.columns[::-1]] ExampleFollowing is the code −import pandas as pd # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, ... Read More
1K+ Views
To remove a column with all null values, use the dropna() method and set the “how” parameter to “all” −how='all'At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a DataFrame. We have set the NaN values using the Numpy np.infdataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": [np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN] } )To remove a column with all null values, use dropna() and set the required parameters −dataFrame.dropna(how='all', axis=1, inplace=True) ExampleFollowing is the code ... Read More
187 Views
To fetch only capital words, we are using regex. The re module is used here and imported. Let us import all the libraries −import re import pandas as pdCreate a DataFrame −data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'], ['KEYBOARD', 'charger', 'SMARTTV', 'camera']] dataFrame = pd.DataFrame(data)Now, extract capital words −for i in range(dataFrame.shape[1]): for ele in dataFrame[i]: if bool(re.match(r'\w*[A-Z]\w*', str(ele))): print(ele)ExampleFollowing is the code −import re import pandas as pd # create a dataframe data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'], ... Read More
320 Views
Use the isin() method to display True for infinite values. At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a dictionary of list. We have set the infinity values using the Numpy np.inf −d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] } Creating DataFrame from the above dictionary of list −dataFrame = pd.DataFrame(d)Display True for infinite values −res = dataFrame.isin([np.inf, -np.inf]) ExampleFollowing is the code −import pandas as pd import numpy as np # dictionary of list d = { "Reg_Price": [7000.5057, np.inf, 5000, ... Read More
1K+ Views
To check and display row index, use the isinf() with any(). At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a dictionary of list. We have set the infinity values using the Numpy np.inf −d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] } Creating DataFrame from the above dictionary of list −dataFrame = pd.DataFrame(d)Getting row index with infinity values −indexNum = dataFrame.index[np.isinf(dataFrame).any(1)] ExampleFollowing is the code −import pandas as pd import numpy as np # dictionary of list d = { "Reg_Price": [7000.5057, np.inf, ... Read More