Assigning value to pandas column not working
Question:
I’m checking if a dataframe is empty and then assigning a value if it is. Dataframe has columns "NAME" and "ROLE"
df = pd.DataFrame(columns = ['NAME', 'ROLE'])
if df.empty:
df["NAME"] = "Jake"
After assigning "Jake" to "NAME". The dataframe is still empty like so:
NAME
ROLE
but I want the dataframe to look like this:
NAME
ROLE
Jake
Answers:
As people are saying in the comments, there are no rows in your empty dataframe to assign the value "Jake" to the "Name" column. Showing that in the first example:
df = pd.DataFrame(columns=['Name','Role'])
df['Name'] = 'Jake'
print(df)
I’m guessing instead you want to add a row:
df = pd.DataFrame(columns=['Name','Role'])
df = df.append({'Name':'Jake','Role':None},ignore_index=True)
print(df)
Assigning a scalar to a pandas dataframe sets each value in that column to the scalar. Since you have zero rows, df["NAME"] = "Jake"
doesn’t assign anything. If you assign a list however, the dataframe is extended for that list. To get a single row in the dataframe
df["NAME"] = ["Jake"]
You could create more rows by adding additional values to the list being assigned.
If you want to change more than one row value you can use .loc method in Pandas module:
change_index = data_["sample_column"].sample(150).index # index of rows whose value will change
data_["sample_column"].loc[sample_index] = 1 # value at specified index and specified column is changed or assigned to 1
Note : If you write as data_.loc[sample_index]["sample_column"]. = 1 it is not working! Because ["sample_column"] condition write as before .loc methods.
I’m checking if a dataframe is empty and then assigning a value if it is. Dataframe has columns "NAME" and "ROLE"
df = pd.DataFrame(columns = ['NAME', 'ROLE'])
if df.empty:
df["NAME"] = "Jake"
After assigning "Jake" to "NAME". The dataframe is still empty like so:
NAME | ROLE |
---|---|
but I want the dataframe to look like this:
NAME | ROLE |
---|---|
Jake |
As people are saying in the comments, there are no rows in your empty dataframe to assign the value "Jake" to the "Name" column. Showing that in the first example:
df = pd.DataFrame(columns=['Name','Role'])
df['Name'] = 'Jake'
print(df)
I’m guessing instead you want to add a row:
df = pd.DataFrame(columns=['Name','Role'])
df = df.append({'Name':'Jake','Role':None},ignore_index=True)
print(df)
Assigning a scalar to a pandas dataframe sets each value in that column to the scalar. Since you have zero rows, df["NAME"] = "Jake"
doesn’t assign anything. If you assign a list however, the dataframe is extended for that list. To get a single row in the dataframe
df["NAME"] = ["Jake"]
You could create more rows by adding additional values to the list being assigned.
If you want to change more than one row value you can use .loc method in Pandas module:
change_index = data_["sample_column"].sample(150).index # index of rows whose value will change
data_["sample_column"].loc[sample_index] = 1 # value at specified index and specified column is changed or assigned to 1
Note : If you write as data_.loc[sample_index]["sample_column"]. = 1 it is not working! Because ["sample_column"] condition write as before .loc methods.