xxxxxxxxxx
air_quality["london_mg_per_cubic"] = air_quality["station_london"] * 1.882
In [5]: air_quality.head()
Out[5]:
station_antwerp london_mg_per_cubic
datetime
2019-05-07 02:00:00 NaN 43.286
2019-05-07 03:00:00 50.5 35.758
2019-05-07 04:00:00 45.0 35.758
2019-05-07 05:00:00 NaN 30.112
2019-05-07 06:00:00 NaN NaN
[5 rows x 4 columns]
xxxxxxxxxx
import pandas as pd
# Assuming we have a dataframe named 'df' and we want to create a new column 'new_column'
# Method 1: Assigning a value to all rows in the new column
df['new_column'] = 'some value'
# Method 2: Assigning values using calculations based on other columns
df['new_column'] = df['column1'] + df['column2']
# Method 3: Assigning values using conditions
df['new_column'] = df['column'].apply(lambda x: 'yes' if x > 0 else 'no')
# Method 4: Assigning values using a function
def some_function(row):
# perform some calculations
return result
df['new_column'] = df.apply(some_function, axis=1)
xxxxxxxxxx
# Import pandas package
import pandas as pd
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Height': [5.1, 6.2, 5.1, 5.2],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# Declare a list that is to be converted into a column
address = ['Delhi', 'Bangalore', 'Chennai', 'Patna']
# Using 'Address' as the column name
# and equating it to the list
df['Address'] = address
# Observe the result
print(df)