Categories
Python Answers

How to display Python Pandas DataFrame of floats using a format string for columns?

To display Python Pandas DataFrame of floats using a format string for columns, we use the map method.

For instance, we write

import pandas as pd
df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
                  index=['foo','bar','baz','quux'],
                  columns=['cost'])
df['foo'] = df['cost']
df['cost'] = df['cost'].map('${:,.2f}'.format)

to create a data frame df with DataFrame.

Then we call call map on the df['cost'] column to return a string with the value interpolated to the format string with format.

Categories
Python Answers

How to find the difference between two data frames with Python Pandas?

To find the difference between two data frames with Python Pandas, we can use the drop_duplicates method.

For instance, we write

pd.concat([df1,df2]).drop_duplicates(keep=False)

to combine the 2 data frames together with concat.

And then we call drop_duplicates to remove the items that are in both data frames.

Categories
Python Answers

How to split text in a column into multiple rows with Python Pandas?

To split text in a column into multiple rows with Python Pandas, we can use the str.split method.

For instance, we write

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {'CustNum': [32363, 31316],
     'CustomerName': ['McCartney, Paul', 'Lennon, John'],
     'ItemQty': [3, 25],
     'Item': ['F04', 'F01'],
     'Seatblocks': ['2:218:10:4,6', '1:13:36:1,12 1:13:37:1,13'],
     'ItemExt': [60, 360]
    }
)
df['Seatblocks'] = df['Seatblocks'].str.split('[ :]')
df = df.explode('Seatblocks').reset_index(drop=True)
cols = list(df.columns)
cols.append(cols.pop(cols.index('CustomerName')))
df = df[cols]

to create the df data frame.

Then we call str.split on df['Seatblocks']. to split the Seatblovks column values by the :.

Then we call exploded to fill the split column values into multiple rows.

And then we create the cols columns list with list.

Next, we call cols.append to append the CustomerName column.

And then we assign the df with the cols columns back to df.

Categories
Python Answers

How to pass another entire column as argument to Python Pandas fillna()?

To pass another entire column as argument to Python Pandas fillna(), we call fillna with the column directly.

For instance, we write

df['Cat1'].fillna(df['Cat2'])

to call fillna with df['Cat2'] to pass the column as the argument to replace NaN values with the values from the Cat2 column.

Categories
Python Answers

How to explode a list inside a Dataframe cell into separate rows with Python Pandas?

To explode a list inside a Dataframe cell into separate rows with Python Pandas, we call the met method.

For instance, we write

pd.melt(df.nearest_neighbors.apply(pd.Series).reset_index(), 
             id_vars=['name', 'opponent'],
             value_name='nearest_neighbors')
     .set_index(['name', 'opponent'])
     .drop('variable', axis=1)
     .dropna()
     .sort_index()

to call melt with the nearest_neighbors column.

We call nearest_neighbors.apply to get the name and opponent column values.

And then we call drop to drop the variable column.

dropna removes the NaN values.

And sort_index sorts the values by the index values.