Given the data framework below:
import pandas as pd
import numpy as np
a = np.arange(16).reshape(4, 4)
df = pd.DataFrame(data=a, columns=['a','b','c','d'])
I would like to get the following result:
df([[ NaN, 1, 2, 3],
[ NaN, NaN, 6, 7],
[ NaN, NaN, NaN, 11],
[ NaN, NaN, NaN, NaN]])
So far I tried to use it np.tril_indicies, but it only works with returning df to the numpy array and only works for whole purposes (not np.nan):
il1 = np.tril_indices(4)
a[il1] = 0
gives:
array([[ 0, 1, 2, 3],
[ 0, 0, 6, 7],
[ 0, 0, 0, 11],
[ 0, 0, 0, 0]])
... this is almost what I'm looking for, but barfs when assigning NaN:
ValueError: cannot convert float NaN to integer
and
df[il1] = 0
gives:
TypeError: unhashable type: 'numpy.ndarray'
So, if I want to fill the bottom triangle of the DataFrame with NaN, then it 1) must be a numpy array, or can I do it using pandas directly? And 2) Is there a way to fill the bottom NaN triangle, and not use numpy.fill_diagonaland increase the line-by-line offset across the entire DataFrame?
:
np , np.nan. NaN, !