mdivk
mdivk

Reputation: 3727

Error when renaming columns to an existing Pandas Dataframe

Basically this question is related to: notebook-memory-usage, the original post is a bit too old, so I guess it is better to start a new one.

Here is portion of code:

def get_proc_info():
    # memory info from psutil.Process
    df_mem = []
    ....
    df_mem = pd.DataFrame(df_mem)
    df_mem.columns = ['user', 'pid', 'memory_GB', 'kernel_ID']
    return df_mem, ports

Errors out at df_mem.columns = ['user', 'pid', 'memory_GB', 'kernel_ID'] as below:

File "./notebook_mem_usage.py", line 64, in get_proc_info df_mem.columns = ['user', 'pid', 'memory_GB', 'kernel_ID']

ValueError: Length mismatch: Expected axis has 0 elements, new values have 4 elements

It would be appreciated if anyone can shed me more light, thank you very much in advance.

Upvotes: 1

Views: 1881

Answers (1)

jpp
jpp

Reputation: 164673

The columns need to exist beforehand before your rename them. Consider an empty dataframe:

df = pd.DataFrame()

print(df.columns)

Index([], dtype='object')

You can't fit 4 values in an array of size 0. Therefore, you see ValueError. You have a couple of options:

Specify columns when instantiating

The pd.DataFrame constructor has a columns argument:

df = pd.DataFrame(columns=['user', 'pid', 'memory_GB', 'kernel_ID'])

Join empty dataframe with one which has columns

Create an empty dataframe and join a new one:

df = pd.DataFrame()
df = df.join(pd.DataFrame(columns=['user', 'pid', 'memory_GB', 'kernel_ID']))

Upvotes: 1

Related Questions