1

I created an empty dataframe and tried adding one string value to a column but it is showing empty

import pandas as pd
df = pd.DataFrame()
df['Man'] = "manish"
print(df)

when i am running above code i am getting output as:

Empty DataFrame
Columns: [Man]
Index: []

While when i am running below code

df['Man'] = ['manish']
print(df)

i am getting correct output which i expected

      Man
0  manish

Can anyone explain me why this is happening ?

5
  • This df['Man'] == "manish" is comparison. Commented Nov 14, 2019 at 20:53
  • You should have got an error with the first snippet. == (two equal signs) is a test for equality. = is an assign (this is what you want here) Commented Nov 14, 2019 at 20:53
  • Probably because the index is empty Commented Nov 14, 2019 at 20:55
  • 1
    The first case broadcasts the value across the row index. As the index is empty it adds no values, but adds the key to the column index. In the second case, by specifying [] you indicate there's a length of 1. IMO it's odd that pandas allows this to work because len(df) == 0 and len(['manish']) == 1. In all cases where the index has a positive size and they mismatch you'd receive ValueError: Length of values does not match length of index Commented Nov 14, 2019 at 20:58
  • Sorry df['Man'] == "manish" i added by mistake , even with df['Man'] = "manish" same issue persists Commented Nov 14, 2019 at 21:04

1 Answer 1

2

It seems, by looking at the code of __setitem__ that it expects a list like value, as written in the function _ensure_valid_index:

def _ensure_valid_index(self, value):
    """
    Ensure that if we don't have an index, that we can create one from the
    passed value.
    """
    # GH5632, make sure that we are a Series convertible
    if not len(self.index) and is_list_like(value):
        try:
            value = Series(value)
        except (ValueError, NotImplementedError, TypeError):
            raise ValueError(
                "Cannot set a frame with no defined index "
                "and a value that cannot be converted to a "
                "Series"
            )

        self._data = self._data.reindex_axis(
            value.index.copy(), axis=1, fill_value=np.nan
        )

So if the len of the index is zero (as in your case) it expects a list like value, which a string is not, to convert to a Series an use the index from there. The function _ensure_valid_index is called inside set_item.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.