Create a Pandas Dataframe by appending one row at a time

How do I create an empty DataFrame, then add rows, one by one? I created an empty DataFrame: df = pd.DataFrame(columns=(‘lib’, ‘qty1’, ‘qty2’)) Then I can add a new row at the end and fill a single field with: df = df._set_value(index=len(df), col=”qty1″, value=10.0) It works for only one field at a time. What is … Read more

Change column type in pandas

I want to convert a table, represented as a list of lists, into a pandas DataFrame. As an extremely simplified example: a = [[‘a’, ‘1.2’, ‘4.2’], [‘b’, ’70’, ‘0.03’], [‘x’, ‘5’, ‘0’]] df = pd.DataFrame(a) What is the best way to convert the columns to the appropriate types, in this case columns 2 and 3 … Read more

How do I get the row count of a Pandas DataFrame?

How do I get the number of rows of a pandas dataframe df? 1 15 For a dataframe df, one can use any of the following: len(df.index) df.shape[0] df[df.columns[0]].count() (== number of non-NaN values in first column) Code to reproduce the plot: import numpy as np import pandas as pd import perfplot perfplot.save( “out.png”, setup=lambda … Read more

Renaming column names in Pandas

How do I change the column labels of a pandas DataFrame from: [‘$a’, ‘$b’, ‘$c’, ‘$d’, ‘$e’] to [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]. 3 32 RENAME SPECIFIC COLUMNS Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed: df = df.rename(columns={‘oldName1’: ‘newName1’, ‘oldName2’: ‘newName2’}) # Or … Read more