COMBINING PANDAS DATAFRAMES
Pandas supports the “concat” method in DataFrames in order to concatenate DataFrames. Listing 3.13 displays the contents of concat_frames.py that illustrates how to combine two Pandas DataFrames.
LISTING 3.13: concat_frames.py
import pandas as pd
can_weather = pd.DataFrame({
"city": ["Vancouver","Toronto","Montreal"],
"temperature": [72,65,50],
"humidity": [40, 20, 25]
})
us_weather = pd.DataFrame({
"city": ["SF","Chicago","LA"],
"temperature": [60,40,85],
"humidity": [30, 15, 55]
})
df = pd.concat([can_weather, us_weather])
print(df)
The first line in Listing 3.13 is an import statement, followed by the definition of the Pandas DataFrames can_weather and us_weather that contain weather-related information for cities in Canada and the United States, respectively. The Pandas DataFrame df is the concatenation...