PART-I: (Merging and Joining)
pd.merge()
methodpd.merge()
Methodindicator
parameter to indicate the df to which the value belongsuffixes
parameter to differentiate between common column labelsvalidate
parameter to check for duplicate keysPART-II: (Concatenating and Appending)
Row wise Concatenation using pd.concat()
Column wise Concatenation using pd.concat()
Adding a Single Row/Column in a Dataframe using pd.concat()
Appending Dataframes using df.append()
pd.merge()
Method¶Pandas pd.merge()
is a versatile method to perform all standard database join operations between DataFrame or named Series objects.
pd.merge(left, right, how="inner", indicator=False, on=None, suffixes=("_x", "_y"), validate=None)
Where,
left
: A DataFrame or named Series object.right
: Another DataFrame or named Series object.how
: specifies the type of join {inner
, outer
, left
, right
} (default is inner
)on
: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects.indicator
: If set to True, adds a column to the output DataFrame called _merge
with information on the source of each row {left_only
means, this element is present only in left Dataframe, right_only
means this is present only in right dataframe, both
means they are present in bothsuffixes
: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y').validate
: If specified, checks for uniqueness of keys. This parameter can take following four values (default is None):
It is the most common type of join you’ll be working with. It returns a dataframe with only those rows that have common characteristics. An inner join requires each row in the two joined dataframes to have matching column values. This is similar to the intersection of two sets.
# Let us create a simple data frame
import pandas as pd
# This dataframe doesn't have entry for Lahore
df_temp = pd.DataFrame({
'city': ['Lahore', 'Muree', 'Peshawer', 'Sialkot'],
'temperature' : [39, 14, 29, 32]
})
df_temp
city | temperature | |
---|---|---|
0 | Lahore | 39 |
1 | Muree | 14 |
2 | Peshawer | 29 |
3 | Sialkot | 32 |
import pandas as pd
# This Dataframe has an extra entry for Multan
df_hum = pd.DataFrame({
'city': [ 'Karachi', 'Lahore', 'Peshawer', 'Lahore', 'Muree'],
'humidity' : [76, 95, 72, 70, 75]
})
df_hum
city | humidity | |
---|---|---|
0 | Karachi | 76 |
1 | Lahore | 95 |
2 | Peshawer | 72 |
3 | Lahore | 70 |
4 | Muree | 75 |
Note the column city
on which we want to perform an inner join, in the two dataframes has only four cities in common. So the resulting dataframe will have only four rows that are common in both dataframes
d1 = pd.merge(df_temp, df_hum, how='inner')
d1
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 39 | 95 |
1 | Lahore | 39 | 70 |
2 | Muree | 14 | 75 |
3 | Peshawer | 29 | 72 |
# merge will perform only for those cities that are common in both, which means it by-default performs inner-join
d1 = pd.merge(df_temp, df_hum, on='city', how = 'inner', indicator=True)
d1
city | temperature | humidity | _merge | |
---|---|---|---|---|
0 | Lahore | 39 | 95 | both |
1 | Lahore | 39 | 70 | both |
2 | Muree | 14 | 75 | both |
3 | Peshawer | 29 | 72 | both |
# Note only the sequence of o/p dataframe changes once we change the order in case of inner join
d1 = pd.merge(df_hum, df_temp, on='city', how = 'inner', indicator=True)
d1
city | humidity | temperature | _merge | |
---|---|---|---|---|
0 | Lahore | 95 | 39 | both |
1 | Lahore | 70 | 39 | both |
2 | Peshawer | 72 | 29 | both |
3 | Muree | 75 | 14 | both |
Also known as Full Outer Join, returns all those records which either have a match in the left or right dataframe. This is similar to the union of two sets.
df_temp = pd.DataFrame({
'city': ['Lahore', 'Muree', 'Peshawer', 'Sialkot'],
'temperature' : [39, 14, 29, 32],
})
df_temp
df_hum = pd.DataFrame({
'city': [ 'Karachi', 'Lahore', 'Peshawer', 'Lahore', 'Muree'],
'humidity' : [76, 95, 72, 70, 75],
})
df_hum
Note the column city
on which we want to perform a full outer join, in the two dataframes has a union of seven cities. So the resulting dataframe will have seven rows
d2 = pd.merge(df_temp, df_hum, on='city', how='outer', indicator=True)
d2
# Note only the sequence of o/p dataframe changes once we change the order in case of inner join
d3 = pd.merge(df_hum, df_temp, on='city', how='outer', indicator=True)
d3
Also known as Left outer join. It is simply performs an inner join plus all the non-matching rows of the left dataframe are taken as it is filled with NaN for columns of the right dataframe.
df_temp = pd.DataFrame({
'city': ['Lahore', 'Muree', 'Peshawer', 'Sialkot'],
'temperature' : [39, 14, 29, 32],
})
df_temp
df_hum = pd.DataFrame({
'city': [ 'Karachi', 'Lahore', 'Peshawer', 'Lahore', 'Muree'],
'humidity' : [76, 95, 72, 70, 75],
})
df_hum
Note the column city
on which we want to perform a left outer join, in the two dataframes has an intersection of four rows. Other than these four rows, record of city Sialkot from left dataframe will also be included in the resulting dataframe being a left outer join
# In left outer join, it takes all the rows from left dataframe and only common rows from right dataframe
d3 = pd.merge(df_temp, df_hum, on='city', how='left', indicator=True)
d3
Left and right outer join also depend on the order of Dataframes that are passed to merge() function. Let us change the order and understand this
d4 = pd.merge(df_hum, df_temp, on='city', how='left', indicator=True)
d4
Also known as Right outer join. It is simply performs an inner join plus all the non-matching rows of the right dataframe are taken as it is filled with NaN for columns of the left dataframe.
import pandas as pd
df_temp = pd.DataFrame({
'city': ['Lahore', 'Muree', 'Peshawer', 'Sialkot'],
'temperature' : [39, 14, 29, 32],
})
df_temp
city | temperature | |
---|---|---|
0 | Lahore | 39 |
1 | Muree | 14 |
2 | Peshawer | 29 |
3 | Sialkot | 32 |
df_hum = pd.DataFrame({
'city': [ 'Karachi', 'Lahore', 'Peshawer', 'Lahore', 'Muree'],
'humidity' : [76, 95, 72, 70, 75],
})
df_hum
city | humidity | |
---|---|---|
0 | Karachi | 76 |
1 | Lahore | 95 |
2 | Peshawer | 72 |
3 | Lahore | 70 |
4 | Muree | 75 |
Note the column city
on which we want to perform a right outer join, in the two dataframes has an intersection of four rows. Other than these four rows, record of city Karachi from right dataframe will also be included in the resulting dataframe being a right outer join
# In Right outer join, it takes all the rows from Right dataframe and only common rows from left dataframe
df3 = pd.merge(df_temp, df_hum, on='city', how='right', indicator=True)
df3
city | temperature | humidity | _merge | |
---|---|---|---|---|
0 | Karachi | NaN | 76 | right_only |
1 | Lahore | 39.0 | 95 | both |
2 | Peshawer | 29.0 | 72 | both |
3 | Lahore | 39.0 | 70 | both |
4 | Muree | 14.0 | 75 | both |
Left and right outer join also depend on the order of Dataframes that are passed to merge() function. Let us change the order and understand this
df4 = pd.merge(df_hum, df_temp, on='city', how='right', indicator=True)
df4
city | humidity | temperature | _merge | |
---|---|---|---|---|
0 | Lahore | 95.0 | 39 | both |
1 | Lahore | 70.0 | 39 | both |
2 | Muree | 75.0 | 14 | both |
3 | Peshawer | 72.0 | 29 | both |
4 | Sialkot | NaN | 32 | right_only |
pd.merge()
Method¶suffixes
Parameter¶_x
, _y
) with column labels to differentiate b/w columns of both dataframessuffixes
.....df1 = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer'],
'temperature' : [35, 39, 33],
'humidity' : [76, 95, 72]
})
df1
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 35 | 76 |
1 | Karachi | 39 | 95 |
2 | Peshawer | 33 | 72 |
df2 = pd.DataFrame({
'city': [ 'Karachi', 'Peshawer', 'Islamabad'],
'temperature' : [41, 44, 47],
'humidity' : [88, 99, 79]
})
df2
city | temperature | humidity | |
---|---|---|---|
0 | Karachi | 41 | 88 |
1 | Peshawer | 44 | 99 |
2 | Islamabad | 47 | 79 |
df3 = pd.merge(df1, df2, on='city', how='inner')
df3
city | temperature_x | humidity_x | temperature_y | humidity_y | |
---|---|---|---|---|---|
0 | Karachi | 39 | 95 | 41 | 88 |
1 | Peshawer | 33 | 72 | 44 | 99 |
merge
has automatically appended suffixes with column labels to differentiate b/w columns of both dataframessuffixes
parameter to pd.merge()
method to specify the suffixes other than _x
and _y
to something more meaningful.d3 = pd.merge(df1, df2, on='city', how='inner', suffixes=('_left','_right'))
d3
city | temperature_left | humidity_left | temperature_right | humidity_right | |
---|---|---|---|---|---|
0 | Karachi | 39 | 95 | 41 | 88 |
1 | Peshawer | 33 | 72 | 44 | 99 |
validate
Parameter to Check for Duplicate Keys¶validate
parameter to the pd.merge()
method to check for uniqueness of keys. This parameter can take following four values (default is None):one_to_one
or 1:1
: checks if merge keys are unique in both left and right datasets.one_to_many
or 1:m
: checks if merge keys are unique in left dataset.many_to_one
or m:1
: checks if merge keys are unique in right dataset.many_to_many
or m:m
: allowed, but does not result in checks.df1 = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Muree'],
'temperature' : [35, 39, 15],
})
df1
city | temperature | |
---|---|---|
0 | Lahore | 35 |
1 | Karachi | 39 |
2 | Muree | 15 |
df2 = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Islamabad', 'Lahore'],
'humidity' : [76, 95, 72, 76],
})
df2
city | humidity | |
---|---|---|
0 | Lahore | 76 |
1 | Karachi | 95 |
2 | Islamabad | 72 |
3 | Lahore | 76 |
df1 = pd.concat([df1, df2] , ignore_index=True)
df1
city | humidity | |
---|---|---|
0 | Lahore | NaN |
1 | Karachi | NaN |
2 | Muree | NaN |
3 | Lahore | NaN |
4 | Karachi | NaN |
5 | Islamabad | NaN |
6 | Lahore | NaN |
7 | Lahore | 76.0 |
8 | Karachi | 95.0 |
9 | Islamabad | 72.0 |
10 | Lahore | 76.0 |
df2 = pd.concat([df1, df2], join='outer', ignore_index=True )
df2
city | humidity | |
---|---|---|
0 | Lahore | NaN |
1 | Karachi | NaN |
2 | Muree | NaN |
3 | Lahore | 76.0 |
4 | Karachi | 95.0 |
5 | Islamabad | 72.0 |
6 | Lahore | 76.0 |
7 | Lahore | 76.0 |
8 | Karachi | 95.0 |
9 | Islamabad | 72.0 |
10 | Lahore | 76.0 |
11 | Lahore | 76.0 |
12 | Karachi | 95.0 |
13 | Islamabad | 72.0 |
14 | Lahore | 76.0 |
15 | Lahore | NaN |
16 | Karachi | NaN |
17 | Muree | NaN |
18 | Lahore | 76.0 |
19 | Karachi | 95.0 |
20 | Islamabad | 72.0 |
21 | Lahore | 76.0 |
22 | Lahore | 76.0 |
23 | Karachi | 95.0 |
24 | Islamabad | 72.0 |
25 | Lahore | 76.0 |
26 | Lahore | 76.0 |
27 | Karachi | 95.0 |
28 | Islamabad | 72.0 |
29 | Lahore | 76.0 |
30 | Lahore | 76.0 |
31 | Karachi | 95.0 |
32 | Islamabad | 72.0 |
33 | Lahore | 76.0 |
34 | Lahore | NaN |
35 | Karachi | NaN |
36 | Muree | NaN |
37 | Lahore | 76.0 |
38 | Karachi | 95.0 |
39 | Islamabad | 72.0 |
40 | Lahore | 76.0 |
41 | Lahore | 76.0 |
42 | Karachi | 95.0 |
43 | Islamabad | 72.0 |
44 | Lahore | 76.0 |
45 | Lahore | 76.0 |
46 | Karachi | 95.0 |
47 | Islamabad | 72.0 |
48 | Lahore | 76.0 |
49 | Lahore | 76.0 |
50 | Karachi | 95.0 |
51 | Islamabad | 72.0 |
52 | Lahore | 76.0 |
one_to_one
or1:1
: checks if merge keys are unique in both left and right dataframes, if not then throw exception
df1, df2
#pd.merge(df1, df2, on='city', how='outer', validate='one_to_one')
one_to_many
or1:m
: checks if merge keys are unique in left dataframe, if not then throw exception
df1, df2
pd.merge(df1, df2, on='city', how='outer', validate='one_to_many')
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 35.0 | 76.0 |
1 | Lahore | 35.0 | 76.0 |
2 | Karachi | 39.0 | 95.0 |
3 | Muree | 15.0 | NaN |
4 | Islamabad | NaN | 72.0 |
many_to_one
orm:1
: checks if merge keys are unique in right dataframe, if not then throw exception
df1, df2
#pd.merge(df1, df2, on='city', how='outer', validate='many_to_one')
many_to_many
orm:m
: No checks are performed on keys uniqueness
df1, df2
pd.merge(df1, df2, on='city', how='outer', validate='many_to_many')
pd.concat()
method is used to concat pandas objects along a particular axis with optional set logic along the other axes.pd.concat(objs, axis=0, join='outer', ignore_index=False, keys=None, verify_integrity=False)
Where,
objs
: a sequence or mapping of Series or DataFrame objectsaxis
: The axis to concatenate along. {0/’index’, 1/’columns’}, default 0join
{‘inner’, ‘outer’}, Default is outer
for union. If inner
that means intersectionignore_index
: If True, the resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. (default is False)keys
: sequence, default None (Construct hierarchical index using the passed keys as the outermost level.)verify_integrity
: boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation.import pandas as pd
Pak_Weather = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'temperature' : [35, 39, 33, 29, 15],
'humidity' : [76, 95, 72, 81, 70],
})
Pak_Weather
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 35 | 76 |
1 | Karachi | 39 | 95 |
2 | Peshawer | 33 | 72 |
3 | Islamabad | 29 | 81 |
4 | Muree | 15 | 70 |
UAE_Weather = pd.DataFrame({
'city': [ 'Dubai', 'Sharja', 'Ajman', 'Abu Dhabi'],
'temperature' : [41, 44, 47, 45],
'humidity' : [88, 99, 79, 86],
})
UAE_Weather
city | temperature | humidity | |
---|---|---|---|
0 | Dubai | 41 | 88 |
1 | Sharja | 44 | 99 |
2 | Ajman | 47 | 79 |
3 | Abu Dhabi | 45 | 86 |
df1 = pd.concat([Pak_Weather, UAE_Weather], join='outer' )
df1
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 35 | 76 |
1 | Karachi | 39 | 95 |
2 | Peshawer | 33 | 72 |
3 | Islamabad | 29 | 81 |
4 | Muree | 15 | 70 |
0 | Dubai | 41 | 88 |
1 | Sharja | 44 | 99 |
2 | Ajman | 47 | 79 |
3 | Abu Dhabi | 45 | 86 |
df1 = pd.concat([Pak_Weather, UAE_Weather])
df1 = pd.concat([Pak_Weather, UAE_Weather], axis=0)
df1
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 35 | 76 |
1 | Karachi | 39 | 95 |
2 | Peshawer | 33 | 72 |
3 | Islamabad | 29 | 81 |
4 | Muree | 15 | 70 |
0 | Dubai | 41 | 88 |
1 | Sharja | 44 | 99 |
2 | Ajman | 47 | 79 |
3 | Abu Dhabi | 45 | 86 |
ignore_index
parameter a value of True
, so that the resulting axis is be labeled 0, …, n - 1.df2 = pd.concat([Pak_Weather,UAE_Weather], axis=0, ignore_index=True)
df2
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 35 | 76 |
1 | Karachi | 39 | 95 |
2 | Peshawer | 33 | 72 |
3 | Islamabad | 29 | 81 |
4 | Muree | 15 | 70 |
5 | Dubai | 41 | 88 |
6 | Sharja | 44 | 99 |
7 | Ajman | 47 | 79 |
8 | Abu Dhabi | 45 | 86 |
keys
argument to pd.concat()
methodignore_index
argument is False
which is the defaultdf3 = pd.concat([Pak_Weather, UAE_Weather], axis=0, keys=["city",])
df3
city | temperature | humidity | ||
---|---|---|---|---|
city | 0 | Lahore | 35 | 76 |
1 | Karachi | 39 | 95 | |
2 | Peshawer | 33 | 72 | |
3 | Islamabad | 29 | 81 | |
4 | Muree | 15 | 70 |
df3 = pd.concat([Pak_Weather, UAE_Weather], axis=0, keys=["Pak", "UAE"])
df3
city | temperature | humidity | ||
---|---|---|---|---|
Pak | 0 | Lahore | 35 | 76 |
1 | Karachi | 39 | 95 | |
2 | Peshawer | 33 | 72 | |
3 | Islamabad | 29 | 81 | |
4 | Muree | 15 | 70 | |
UAE | 0 | Dubai | 41 | 88 |
1 | Sharja | 44 | 99 | |
2 | Ajman | 47 | 79 | |
3 | Abu Dhabi | 45 | 86 |
df.loc
to get a subset of your dataframedf3.loc['Pak', :]
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 35 | 76 |
1 | Karachi | 39 | 95 |
2 | Peshawer | 33 | 72 |
3 | Islamabad | 29 | 81 |
4 | Muree | 15 | 70 |
df3.loc['UAE', :]
city | temperature | humidity | |
---|---|---|---|
0 | Dubai | 41 | 88 |
1 | Sharja | 44 | 99 |
2 | Ajman | 47 | 79 |
3 | Abu Dhabi | 45 | 86 |
Pak_Weather = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'temperature' : [35, 39, 33, 29, 15],
})
Pak_Weather
city | temperature | |
---|---|---|
0 | Lahore | 35 |
1 | Karachi | 39 |
2 | Peshawer | 33 |
3 | Islamabad | 29 |
4 | Muree | 15 |
UAE_Weather = pd.DataFrame({
'city': [ 'Dubai', 'Sharja', 'Ajman', 'Abu Dhabi'],
'temperature' : [41, 44, 47, 45],
'humidity' : [88, 99, 79, 86],
})
UAE_Weather
city | temperature | humidity | |
---|---|---|---|
0 | Dubai | 41 | 88 |
1 | Sharja | 44 | 99 |
2 | Ajman | 47 | 79 |
3 | Abu Dhabi | 45 | 86 |
# NaN will be placed where values are missing
df = pd.concat([Pak_Weather,UAE_Weather], axis=0, ignore_index=True)
df
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 35 | NaN |
1 | Karachi | 39 | NaN |
2 | Peshawer | 33 | NaN |
3 | Islamabad | 29 | NaN |
4 | Muree | 15 | NaN |
5 | Dubai | 41 | 88.0 |
6 | Sharja | 44 | 99.0 |
7 | Ajman | 47 | 79.0 |
8 | Abu Dhabi | 45 | 86.0 |
axis=1
to do the job.temp_df = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'temperature' : [35, 39, 33, 29, 15],
})
temp_df
wind_df = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'wind speed' : [9, 12, 7, 13, 18],
})
wind_df
# We have to use the argument axis=1
df = pd.concat([temp_df,wind_df], axis=1)
df
# This dataframe do not have the temperature for Lahore
temp_df = pd.DataFrame({
'city': [ 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'temperature' : [39, 33, 29, 15],
})
temp_df
#This dataframe do not have the windspeed of Islamabad
wind_df = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Muree'],
'wind speed' : [9, 12, 7, 18],
})
wind_df
df1 = pd.concat([temp_df,wind_df], axis=1)
df1
This doesnot look correct
# This dataframe do not have the temperature for Lahore
temp_df = pd.DataFrame({
'city': [ 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'temperature' : [39, 33, 29, 15],
},index=[0,1,2,3])
temp_df
#This dataframe do not have the windspeed of Islamabad
# Note the indices in wind_df are related to indices of temp_df
wind_df = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Muree'],
'wind speed' : [9, 12, 7, 18],
}, index=[4,0,1,3])
wind_df
df = pd.concat([temp_df,wind_df], axis=1)
df
- Concatenating Dataframes along axis = 1 adds one Dataframe along the other. It is like a full outer join. Placing NaN for non-matching rows in the left as well as right Dataframes.
- By default, a concatenation results in a set union, where all data is preserved.
pd.concat()
method.df1 = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'temperature' : [35, 39, 33, 29, 15],
'humidity' : [76, 95, 72, 81, 70],
})
df1
df2 = pd.DataFrame({"city": "Multan", "temperature": 45, "humidity": 75}, index=[5])
df2
df3 = pd.concat([df1, df2], ignore_index=True, axis = 0)
df3
You can place the new row at your desired location using slicing operator, as shown below
df3 = pd.concat([df1[:2], df2, df1[2:]], ignore_index = True)
df3
Pak_Weather = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'temperature' : [35, 39, 33, 29, 15],
'humidity' : [76, 95, 72, 81, 70],
})
Pak_Weather
s = pd.Series(["Humid", 'Dry', 'Rainy', 'Humid', 'Rainy'], name="event")
s
df = pd.concat([Pak_Weather, s], axis=1)
df
df1.append(df2)
method is used to concat the second dataframe’s records at the end of first dataframe (along axis=0). Columns not present in the first DataFrame are added as new columnsdf1.append(df2)
method considers the calling dataframe as main object and adds rows to that dataframe from the dataframes that are passed to the function as argument.other
. The dataframe that called the append()
method, remain unchanged.df.append(other, ignore_index=False, verify_integrity=False, sort=False)
- `other`: DataFrame or Series/dict-like object, or list of these (The data to append.)
- `ignore_index`: If True, the resulting axis will be labeled 0, 1, …, n - 1 (default is False)
- `verify_integrity`: If True, raise ValueError on creating index with duplicates (default is False)
- `sort`: Sort columns if the columns of `self` and `other` are not aligned (default is False)
Pak_Weather = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'temperature' : [35, 39, 33, 29, 15],
'humidity' : [76, 95, 72, 81, 70],
})
Pak_Weather
UAE_Weather = pd.DataFrame({
'city': [ 'Dubai', 'Sharja', 'Ajman', 'Abu Dhabi'],
'temperature' : [41, 44, 47, 45],
'humidity' : [88, 99, 79, 86],
})
UAE_Weather
# append Dataframe
df2 = Pak_Weather.append(UAE_Weather)
df2
ignore_index
parameter a value of True
, so that the resulting axis is be labeled 0, …, n - 1.# set the ignore_index to true
df2 = Pak_Weather.append(UAE_Weather, ignore_index=True)
df2
Pak_Weather = pd.DataFrame({
'city': [ 'Lahore', 'Karachi', 'Peshawer', 'Islamabad', 'Muree'],
'temperature' : [35, 39, 33, 29, 15],
'humidity' : [76, 95, 72, 81, 70],
})
Pak_Weather
city | temperature | humidity | |
---|---|---|---|
0 | Lahore | 35 | 76 |
1 | Karachi | 39 | 95 |
2 | Peshawer | 33 | 72 |
3 | Islamabad | 29 | 81 |
4 | Muree | 15 | 70 |
# Creating a row to be appended
d1 = pd.DataFrame({"city": "Multan", "temperature": 45, "humidity": 75}, index=[5])
d1
city | temperature | humidity | |
---|---|---|---|
5 | Multan | 45 | 75 |
# Append this dataframe having single row to Pak_Weather dataframe
df3 = Pak_Weather.append(d1)
df3
Columns of passed/other dataframe that are not in the caller are added as new columns
d1 = pd.DataFrame({"city": "Sialkot", "temperature": 45, "humidity": 75, "newcol": 66}, index=[5])
d1
df3 = Pak_Weather.append(d1)
df3