Skip to content

assigning column values to empty dataframe with loc #10017

Closed
@paolini

Description

@paolini

I'm trying to add a column to an existing dataframe. The dataframe can be empty (0 rows) but I want the column to be added anyway.

import pandas as pd
import numpy as np

df = pd.DataFrame(index=np.arange(4))
df.loc[:,'col'] = 42 # this works fine!

df = pd.DataFrame(index=np.arange(0)) # empty dataframe
df.loc[:,'col'] = 42 # ERROR: ValueError: cannot set a frame with no defined index and a scalar
df['col'] = 42 # this works fine but seems deprecated

pd.__version__ is '0.15.2'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions