How to create a table from dataframe in python
I currently have a python script that analyzes a jstack dump and outputs a dataframe like this: Show
I want to turn this into a png or jpg image of a table containing this data. Something simple like this: Does anyone know what the easiest, most straight forward way to produce an image like this so it is saved in the same path that I am running the code? Thank you! Javier ***EDIT: The provided solutions are outputting an unreadable table with mostly white space as below: Table output Can anyone suggest what I'm doing wrong and which parameters to adjust? ***EDIT #2: Here is my code:
And the current output: Table output #2 ***EDIT #3: This is the dataframe I am passing: In this short guide, you’ll see two different methods to create Pandas DataFrame:
Method 1: typing values in Python to create Pandas DataFrameTo create Pandas DataFrame in Python, you can follow this generic template: import pandas as pd data = {'first_column': ['first_value', 'second_value', ...], 'second_column': ['first_value', 'second_value', ...], .... } df = pd.DataFrame(data) print (df) Note that you don’t need to use quotes around numeric values (unless you wish to capture those values as strings). Now let’s see how to apply the above template using a simple example. To start, let’s say that you have the following data about products, and that you want to capture that data in Python using Pandas DataFrame:
You may then use the code below in order to create the DataFrame for our example: import pandas as pd data = {'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'], 'price': [1200, 150, 300, 450, 200] } df = pd.DataFrame(data) print (df) Run the code in Python, and you’ll get the following DataFrame:
You may have noticed that each row is represented by a number (also known as the index) starting from 0. Alternatively, you may assign another value/name to represent each row. For example, in the code below, the index=[‘product_1′,’product_2′,’product_3′,’product_4′,’product_5’] was added: import pandas as pd data = {'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'], 'price': [1200, 150, 300, 450, 200] } df = pd.DataFrame(data, index=['product_1','product_2','product_3','product_4','product_5']) print (df) You’ll now see the newly assigned index (as highlighted in yellow):
Let’s now review the second method of importing the values into Python to create the DataFrame. Method 2: importing values from a CSV file to create Pandas DataFrameYou may use the following template to import a CSV file into Python in order to create your DataFrame: import pandas as pd data = pd.read_csv(r'Path where the CSV file is stored\File name.csv') df = pd.DataFrame(data) print (df) Let’s say that you have the following data stored in a CSV file (where the CSV file name is ‘products’):
In the Python code below, you’ll need to change the path name to reflect the location where the CSV file is stored on your computer. For example, let’s suppose that the CSV file is stored under the following path: ‘C:\Users\Ron\Desktop\products.csv’ Here is the full Python code for our example: import pandas as pd data = pd.read_csv(r'C:\Users\Ron\Desktop\products.csv') df = pd.DataFrame(data) print (df) As before, you’ll get the same Pandas DataFrame in Python:
You can also create the same DataFrame by importing an Excel file into Python using Pandas. Find the maximum value in the DataFrameOnce you have your values in the DataFrame, you can perform a large variety of operations. For example, you may calculate stats using Pandas. For instance, let’s say that you want to find the maximum price among all the products within the DataFrame. Obviously, you can derive this value just by looking at the dataset, but the method presented below would work for much larger datasets. To get the maximum price for our example, you’ll need to add the following portion to the Python code (and then print the results): max_price = df['price'].max() Here is the complete Python code: import pandas as pd data = {'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'], 'price': [1200, 150, 300, 450, 200] } df = pd.DataFrame(data) max_price = df['price'].max() print (max_price) Once you run the code, you’ll get the value of 1200, which is indeed the maximum price:
You may check the Pandas Documentation to learn more about creating a DataFrame. How do you create a data frame table in python?Method - 3: Create Dataframe from dict of ndarray/lists. import pandas as pd.. # assign data of lists.. data = {'Name': ['Tom', 'Joseph', 'Krish', 'John'], 'Age': [20, 21, 19, 18]}. # Create DataFrame.. df = pd.DataFrame(data). # Print the output.. print(df). How do you create a data frame from a DataFrame?There are three common ways to create a new pandas DataFrame from an existing DataFrame:. Method 1: Create New DataFrame Using Multiple Columns from Old DataFrame new_df = old_df[['col1','col2']]. ... . Method 2: Create New DataFrame Using One Column from Old DataFrame new_df = old_df[['col1']].. How do we create a table in a DataFrame using a .json file?How to do it…. To create a Pandas DataFrame from a JSON file, first import the Python libraries that you need: ... . Next, define a variable for the JSON file and enter the full path to the file: ... . Next, create a DataFrame from the JSON file using the read_json() method provided by Pandas.. Is a table a DataFrame?Despite sharing a similar tabular look, tables and dataframes are defined as different data structures and have different operations available.
|