以各个城市的天气为例, 先准备下面的数据:
印度天气的相关信息:
import pandas as pd
india_weather = pd.DataFrame({
'city': ['mumbai', 'delhi', 'banglore'],
'temperature': [32, 34, 30],
'humidity': [80, 60, 72]
})
india_weather
美国天气的相关信息:
us_weather = pd.DataFrame({
'city': ['newyork', 'chicago', 'orlando'],
'temperature': [21, 24, 32],
'humidity': [68, 65, 70]
})
us_weather
用 concat 组合上面两个 dataframe:
df = pd.concat([india_weather, us_weather])
df
输出:
上面的输出最左边的序列号是重复的, 原因是数据分别来自两个 dataframe 的索引值, 可以通过忽略原本的索引来做改变:
df = pd.concat([india_weather, us_weather], ignore_index=True)
还可以这样输出:
df = pd.concat([i