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
Programming Articles - Page 1073 of 3363
399 Views
When it is required to customize the space size padding in a list of strings, an empty list, an iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "is", "great"] print("The list is :") print(my_list) lead_size = 3 trail_size = 2 my_result = [] for elem in my_list: my_result.append((lead_size * ' ') + elem + (trail_size * ' ')) print("The result is :") print(my_result)OutputThe list is : ['Python', 'is', 'great'] The result is : [' Python ', ' is ', ' great ']ExplanationA list is defined and ... Read More
394 Views
When it is required to find the first occurrence of one list in another list, the ‘set’ attribute and the ‘next’ method is used.ExampleBelow is a demonstration of the samemy_list_1 = [23, 64, 34, 77, 89, 9, 21] my_list_2 = [64, 10, 18, 11, 0, 21] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_list_2 = set(my_list_2) my_result = next((ele for ele in my_list_1 if ele in my_list_2), None) print("The result is :") print(my_result)OutputThe first list is : [23, 64, 34, 77, 89, 9, 21] The second list is : [64, 10, 18, ... Read More
1K+ Views
To merge Pandas DataFrame, use the merge() function. The inner join is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “inner”At first, let us import the pandas library with an alias −import pandas as pd Create DataFrame1 −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Now, create DataFrame2 −dataFrame2 = pd.DataFrame( { ... Read More
1K+ Views
To calculate the variance of column values, use the var() method. At first, import the required Pandas library −import pandas as pdCreate a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Finding Variance of "Units" column values using var() function −print"Variance of Units column from DataFrame1 = ", dataFrame1['Units'].var()In the same way, we have calculated the Variance from the 2nd DataFrame.ExampleFollowing is the complete code −import pandas as pd ... Read More
9K+ Views
To reset index after group by, at first group according to a column using groupby(). After that, use reset_index().At first, import the required library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] } ) Group according to Car column −resDF = dataFrame.groupby("Car").mean()Now, reset index after grouping −resDF.reset_index() ExampleFollowing is the code − import pandas as ... Read More
5K+ Views
To sum only specific rows, use the loc() method. Mention the beginning and end row index using the : operator. Using loc(), you can also set the columns to be included. We can display the result in a new column.At first, let us create a DataFrame. We have Product records in it, including the Opening and Closing Stock −dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900]})Sum of some rows i.e. 1st two rows. Column names also mentioned in the loc() i.e. Opening_Stock and Closing_Stock. We are displaying result in a new ... Read More
718 Views
To calculate the median of column values, use the median() method. At first, import the required Pandas library −import pandas as pdNow, create a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Finding the median of a single column “Units” using median() −print"Median of Units column from DataFrame1 = ", dataFrame1['Units'].median() In the same way, we have calculated the median value from the 2nd DataFrame.ExampleFollowing is the complete code ... Read More
5K+ Views
To find the common rows between two DataFrames, use the merge() method. Let us first create DataFrame1 with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Create DataFrame2 with two columns −dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 250, 150, 80, 130, 90] } )To find the common ... Read More
397 Views
To check if any specific column of two DataFrames are equal or not, use the equals() method. Let us first create DataFrame1 with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Create DataFrame2 with two columns −dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Mercedes', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] ... Read More
782 Views
To calculate the mean of column values, use the mean() method. At first, import the required Pandas library −import pandas as pdNow, create a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Finding the mean of a single column “Units” using mean() −print"Mean of Units column from DataFrame1 = ", dataFrame1['Units'].mean()In the same way, we have calculated the mean value from the 2nd DataFrame.ExampleFollowing is the complete code −import pandas ... Read More