Convert DataFrame into Dictionary in Pandas

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 13:22:41

1K+ Views

To convert a Pandas DataFrame into a dictionary, we can use the to_dict() method. Let's take an example and see how it's done.StepsCreate two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Convert the DataFrame into a dictionary using to_dict() method and print it.Example import pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] } ) print "Input DataFrame is:", df print "Convert DataFrame into dictionary: ", df.to_dict()Output Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 Convert DataFrame into dictionary: {'x': {0: 5, 1: 2, 2: 7, 3: 0}, 'y': {0: 4, 1: 7, 2: 5, 3: 1}, 'z': {0: 9, 1: 3, 2: 5, 3: 1}}

Put Pandas DataFrame into JSON File and Read It Again

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 13:18:13

1K+ Views

To put a Pandas DataFrame into a JSON file and read it again, we can use to_json() and read_json() methods.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Use to_json() method to dump the DataFrame into a JSON file.Use read_json() method to read the JSON file.Exampleimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 7, 0],       "y": [4, 7, 5, 1],       "z": [9, 3, 5, 1] } ) print "Input DataFrame is:", df print "JSON output for input DataFrame: ", df.to_json("test.json") ... Read More

Select DataFrame Rows Between Two Index Values in Python Pandas

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 12:57:27

6K+ Views

We can slice a Pandas DataFrame to select rows between two index values. Let's take an example and see how it's done.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a variable for lower limit of the index.Initialize another variable for upper limit of the index.Use df[index_lower_limit: index_upper_limit] to print the DataFrame in range index.Exampleimport pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, ... Read More

Selecting with Complex Criteria from a Pandas DataFrame

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 12:40:37

185 Views

We can use different criteria to compare all the column values of a Pandas DataFrame. We can perform comparison operations like df[col]2, then it will check all the values from col and compare whether they are greater than 2. For all the column values, it will return True if the condition holds, else False. Let's take an example and see how it's done.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a variable col, with a column name.Perform some comparison operations.Print the resultant DataFrame.Example import pandas as pd df = pd.DataFrame( ... Read More

Check If Two DataFrames Are Exactly Same in Python Pandas

AmitDiwan
Updated on 14-Sep-2021 12:12:17

720 Views

The equals() function is used to check if two dataframes are exactly same. At first, let us 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'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] ... Read More

Count Unique Values per Groups in Python Pandas

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 12:07:37

6K+ Views

To count unique values per groups in Python Pandas, we can use df.groupby('column_name').count().StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Use df.groupby('rank')['id'].count() to find the count of unique values per groups and store it in a variable "count".Print the count from Step 3.Exampleimport pandas as pd df = pd.DataFrame(     {        "id": [1, 2, 1, 3, 5, 1, 4, 3, 6, 7],        'rank': [1, 4, 1, 2, 1, 4, 6, 1, 5, 3]     } ) print"Input DataFrame 1 is:", df count = df.groupby('rank')['id'].count() print"Frequency of ranks:", countOutputInput DataFrame 1 is:    id  rank 0   1    1 1   2    4 2   1    1 3   3    2 4   5    1 5   1    4 6   4    6 7   3    1 8   6    5 9   7    3 Frequency of ranks: rank 1  4 2  1 3  1 4  2 5  1 6  1 Name: id, dtype: int64

Group By and Sum in Python Pandas

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 11:59:23

6K+ Views

To find group-by and sum in Python Pandas, we can use groupby(columns).sum().StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Find the groupby sum using df.groupby().sum(). This function takes a given column and sorts its values. After that, based on the sorted values, it also sorts the values of other columns.Print the groupby sum.Exampleimport pandas as pd df = pd.DataFrame(     {        "Apple": [5, 2, 7, 0],        "Banana": [4, 7, 5, 1],        "Carrot": [9, 3, 5, 1]     } ) print "Input DataFrame 1 ... Read More

Find the Difference Between Two DataFrames in Python Pandas

AmitDiwan
Updated on 14-Sep-2021 11:57:22

803 Views

To find the difference between two DataFrame, you need to check for its equality. Also, check the equality of columns.Let us 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', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ... Read More

Create a Subset of Columns Using Filter in Python

AmitDiwan
Updated on 14-Sep-2021 11:55:22

305 Views

To create a subset of columns, we can use filter(). Through this, we can filter column values with similar pattern using like operator. At first, let us create a DataFrame with 3 columns −dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900]})Now, let us create a subset with multiple columns −dataFrame[['Opening_Stock', 'Closing_Stock']]Create a subset with similarly patterned names −dataFrame.filter(like='Open')ExampleFollowing is the complete code −import pandas as pd dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900]}) print"DataFrame...", dataFrame print"Displaying a subset ... Read More

Get Column Index from Column Name in Python Pandas

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 11:45:05

12K+ Views

To get column index from column name in Python Pandas, we can use the get_loc() method.Steps −Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Find the columns of DataFrame, using df.columns.Print the columns from Step 3.Initialize a variable column_name.Get the location, i.e., of index for column_name.Print the index of the column_name.Example −import pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 7, 0],       "y": [4, 7, 5, 1],       "z": [9, 3, 5, 1]    } ) print"Input DataFrame 1 is:", df columns = ... Read More

Advertisements