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 ?
df['Man'] == "manish"is comparison.==(two equal signs) is a test for equality.=is an assign (this is what you want here)[]you indicate there's a length of 1. IMO it's odd thatpandasallows this to work becauselen(df) == 0andlen(['manish']) == 1. In all cases where the index has a positive size and they mismatch you'd receiveValueError: Length of values does not match length of indexdf['Man'] == "manish"i added by mistake , even withdf['Man'] = "manish"same issue persists