Believe what you are looking for is actually answered here: Add column in dataframe from list
myList = [1, 2, 3, 4, 5] print(len(df)) # 50 df['new_col'] = mylist print(len(df)) # 51
Alternatively, you could set the value of a slice of the list like so:
data['new_col'] = 1
data.loc[2880: 5760, 'new_col'] = 0
data.loc[12960: 15840, 'new_col'] = 0
data.loc[23040: 25920, 'new_col'] = 0
df = pd.DataFrame([i
for i in range(28800)
])
df["new_col"] = 1
zeros_bool = [(
(i >= (2880 - 1) and i < 5760) or(i >= (12960 - 1) and i < 15840) or(i >= (23040 - 1) and i < 25920)
) for i in range(28800)]
df.loc[zeros_bool, "new_col"] = 0
To create a new column, use the [] brackets with the new column name at the left side of the assignment.,The calculation of the values is done element_wise. This means all values in the given column are multiplied by the value 1.882 at once. You do not need to use a loop to iterate each of the rows!,Create a new column by assigning the output to the DataFrame with a new column name in between the [].,The rename() function can be used for both row labels and column labels. Provide a dictionary with the keys the current names and the values the new names to update the corresponding names.
In[1]: import pandas as pd
In[2]: air_quality = pd.read_csv("data/air_quality_no2.csv", index_col = 0, parse_dates = True)
In[3]: air_quality.head()
Out[3]:
station_antwerp station_paris station_london
datetime
2019 - 05 - 07 02: 00: 00 NaN NaN 23.0
2019 - 05 - 07 03: 00: 00 50.5 25.0 19.0
2019 - 05 - 07 04: 00: 00 45.0 27.7 19.0
2019 - 05 - 07 05: 00: 00 NaN 50.4 16.0
2019 - 05 - 07 06: 00: 00 NaN 61.9 NaN
In[4]: air_quality["london_mg_per_cubic"] = air_quality["station_london"] * 1.882
In[5]: air_quality.head()
Out[5]:
station_antwerp station_paris station_london london_mg_per_cubic
datetime
2019 - 05 - 07 02: 00: 00 NaN NaN 23.0 43.286
2019 - 05 - 07 03: 00: 00 50.5 25.0 19.0 35.758
2019 - 05 - 07 04: 00: 00 45.0 27.7 19.0 35.758
2019 - 05 - 07 05: 00: 00 NaN 50.4 16.0 30.112
2019 - 05 - 07 06: 00: 00 NaN 61.9 NaN NaN
In[6]: air_quality["ratio_paris_antwerp"] = (
...: air_quality["station_paris"] / air_quality["station_antwerp"]
...: )
...:
In[7]: air_quality.head()
Out[7]:
station_antwerp station_paris station_london london_mg_per_cubic ratio_paris_antwerp
datetime
2019 - 05 - 07 02: 00: 00 NaN NaN 23.0 43.286 NaN
2019 - 05 - 07 03: 00: 00 50.5 25.0 19.0 35.758 0.495050
2019 - 05 - 07 04: 00: 00 45.0 27.7 19.0 35.758 0.615556
2019 - 05 - 07 05: 00: 00 NaN 50.4 16.0 30.112 NaN
2019 - 05 - 07 06: 00: 00 NaN 61.9 NaN NaN NaN
In[8]: air_quality_renamed = air_quality.rename(
...: columns = {
...: "station_antwerp": "BETR801",
...: "station_paris": "FR04014",
...: "station_london": "London Westminster",
...:
}
...: )
...:
In[9]: air_quality_renamed.head()
Out[9]:
BETR801 FR04014 London Westminster london_mg_per_cubic ratio_paris_antwerp
datetime
2019 - 05 - 07 02: 00: 00 NaN NaN 23.0 43.286 NaN
2019 - 05 - 07 03: 00: 00 50.5 25.0 19.0 35.758 0.495050
2019 - 05 - 07 04: 00: 00 45.0 27.7 19.0 35.758 0.615556
2019 - 05 - 07 05: 00: 00 NaN 50.4 16.0 30.112 NaN
2019 - 05 - 07 06: 00: 00 NaN 61.9 NaN NaN NaN
To do a conditional update depending on whether the current value of a column matches the condition, you can add a WHERE clause which specifies this. The database will first find rows which match the WHERE clause and then only perform updates on those rows.,In this tutorial, we’ll go over the various ways to update rows in a table using SQL progressing from more general updates to more specific methods.,Now what happens if you want to update rows in one table based on the condition of another table? This question leads to a few different ways you could do this.,To expand on this, you can add anything to the WHERE clause you like as long as it’s a valid expression. So to perform an update based on the value of another column in the same table, you could execute the following:
UPDATE table SET col = new_value;
UPDATE table SET col = new_value WHERE col = old_value;
UPDATE table SET col = new_value WHERE other_col = some_other_value;
UPDATE table SET col = new_value WHERE col = old_value AND other_col = some_other_value; UPDATE table SET col = new_value WHERE col = old_value OR other_col = some_other_value;
UPDATE table SET col = new_value WHERE other_col IN( SELECT other_col FROM other_table WHERE conditional_col = 1 );
UPDATE table SET col = ( SELECT other_col FROM other_table WHERE other_table.table_id = table.id );