-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-ConcatDataFrames.py
More file actions
59 lines (37 loc) · 1.2 KB
/
Copy path8-ConcatDataFrames.py
File metadata and controls
59 lines (37 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pandas as pd
india_weather = pd.DataFrame ({
"city" : ["mumbai","delhi","banglore"],
"temperature" : [32,45,30],
"humidity": [80,60,70]
})
print(india_weather)
us_weather = pd.DataFrame ({ #creating dataframe
"city" : ["new york","chicago","orlando"],
"temperature" : [21,14,35],
"humidity": [68,65,75]
})
print(us_weather)
print("")
df = pd.concat([india_weather,us_weather], keys=["india","us"]) # ignore_index=True to get single index for concaneted df
print(df)
print("")
print(df.loc["india"]) #returning indian values from concaneted df
print("")
print(df.loc["us"]) #returning us values from concaneted df
#####
print("")
temperature_df = pd.DataFrame({
"city" : ["mumbai","delhi","banglore"],
"temperature" : [32,45,30]
})
windspeed_df = pd.DataFrame({
"city" : ["delhi","mumbai"],
"windspeed" : [7,12]
}, index=[1,0]) #match index with previous dataframe
df1 = pd.concat([temperature_df,windspeed_df], axis=1) #will append windspeed as additional column
print(df1)
####
print(temperature_df)
s = pd.Series(["Humid","Dry","Rain"], name="event")
df3 = pd.concat([temperature_df, s], axis=1)
print(df3)