27
loading...
This website collects cookies to deliver better user experience
df2 = {'First Name': 'Vikram', 'Last Name': 'Aruchamy', 'Country': 'India'}
df = df.append(df2, ignore_index = True)
df
Country | First Name | Last Name | |
---|---|---|---|
0 | India | Vikram | Aruchamy |
DataFrame()
method as shown below. import pandas as pd
df = pd.DataFrame()
df
df
. append()
, concat()
, iloc[]
and loc[]
. print(df)
to know the dataframe columns.dictionary
or Pandas Series
or Dataframe
- Object with values for new rowignore_index
= True Means the index from the series or the source dataframe will be ignored. The index available in the target dataframe will be used, instead. False means otherwise. This is optional. inplace
append is not possible. Hence, do not forget to assign the result to a dataframe object to access it later. append()
method.dict = {'First Name': 'Vikram', 'Last Name': 'Aruchamy', 'Country': 'India'}
df = df.append(dict, ignore_index = True)
df
Country | First Name | Last Name | |
---|---|---|---|
0 | India | Vikram | Aruchamy |
List of dataframes
- List of dataframes that needs to be concatenated ignore_index
- Whether the index of the new dataframe should be ignored when concatenating to the target dataframe axis = 0
- To denote that rows of the dataframe needs to be converted. If you want to concatenate columns, you can use axis=1
inplace
concatenation is not supported. Hence, remember to assign the result to a variable for later use. df2 = pd.DataFrame({'First Name': ['Kumar'],
'Last Name' : ['Ram'],
'Country' : ['India']})
df = pd.concat([df, df2], ignore_index = True, axis = 0)
df
df2
. You're concatenating this to dataframe df
which already has one dataframe in it. df
and df2
will be concatenated and you'll see two rows in the resultant dataframe. Country | First Name | Last Name | |
---|---|---|---|
0 | India | Vikram | Aruchamy |
1 | India | Kumar | Ram |
concat()
method to add rows to the dataframe. iLoc
to add a row, the dataframe must already have a row in the position. Atleast an empty row. If a row is not available, you'll see an error IndexError: iloc cannot enlarge its target object
. iLoc will not expand the size of the dataframe automatically. df.iloc[1] = ['Shivam', 'Pandey', 'India']
df
1
. It replaced the values available in that position with the new values. Country | First Name | Last Name | |
---|---|---|---|
0 | India | Vikram | Aruchamy |
1 | Shivam | Pandey | India |
iloc[]
to insert a row to the existing dataframe. loc[]
is used to access set of rows from the dataframe using the index label. You can also assign rows with a specific index label using the loc
attribute. loc[]
attribute, its not mandatory that a row already exists with specific label. It'll automatically extend the dataframe and add a row with that label, unlike the iloc[]
method. a
, b
, a new dataframe is created with labels a
and b
. Then a new row is assigned with the row label c
using the loc[]
method. import pandas as pd
# List of Tuples
users = [ ('Shivam', 'Pandey', 'India'),
('Kumar', 'Ram' , 'India' ),
]
#Create a DataFrame object
df3 = pd.DataFrame( users,
columns = ['First Name' , 'Last Name', 'Country'],
index=['a', 'b'])
print('Dataframe before adding a new row:\n')
print('---------------------------------------\n')
print(df3)
df3.loc['c'] = ['Vikram', 'Aruchamy', 'India']
print('\nDataframe after adding a new row:\n')
print('---------------------------------------\n')
print(df3)
df3
is created with two rows with label a
and b
. Then a row is inserted with label c
using the loc[]
method. Dataframe before adding a new row:
---------------------------------------
First Name Last Name Country
a Shivam Pandey India
b Kumar Ram India
Dataframe after adding a new row:
---------------------------------------
First Name Last Name Country
a Shivam Pandey India
b Kumar Ram India
c Vikram Aruchamy India
loc[]
method to add rows to the dataframe. Either it is an empty dataframe or it already has values. loc
, iloc
, append()
or concat()
methods to add rows to the dataframe. loc
method. iloc[]
method to add rows at a specific index. However, in that case, there must be a row already existing with specific index. loc[]
, If a row is already existing with that index label, it'll be replaced with the new row. df.loc[2] = ['Shivam', 'Pandey', 'India']
df
2
. Country | First Name | Last Name | |
---|---|---|---|
0 | India | Vikram | Aruchamy |
1 | Shivam | Pandey | India |
2 | Shivam | Pandey | India |
df.loc[-1]
.-1
, you can increment all the indexes by 1
. df.loc[-1] = ['Raj', 'Kumar', 'India']
df.index = df.index + 1
df = df.sort_index()
df
-1
and then all the indexes will be incremented and sorted. Country | First Name | Last Name | |
---|---|---|---|
0 | Raj | Kumar | India |
1 | India | Vikram | Aruchamy |
2 | Shivam | Pandey | India |
3 | Shivam | Pandey | India |
df.loc[df.shape[0]]
.df.shape[0]
returns the length of the dataframe. 4
. Hence when you insert using loc[4]
, a row will be added at bottom of the dataframe which is index 4
. df.loc[df.shape[0]] = ['Krishna', 'Kumar', 'India']
df
Country | First Name | Last Name | |
---|---|---|---|
0 | Raj | Kumar | India |
1 | India | Vikram | Aruchamy |
2 | Shivam | Pandey | India |
3 | Shivam | Pandey | India |
4 | Krishna | Kumar | India |
loc[]
. df.loc[df.shape[0]]
and assigning None
values for all the existing columns. None
values and assign it at the last position of the dataframe. df.loc[df.shape[0]] = [None, None, None]
df
Country | First Name | Last Name | |
---|---|---|---|
0 | Raj | Kumar | India |
1 | India | Vikram | Aruchamy |
2 | Shivam | Pandey | India |
3 | Shivam | Pandey | India |
4 | Krishna | Kumar | India |
5 | None | None | None |
append()
method. append()
method accepts a dataframe and appends it to the calling dataframe and returns a new dataframe object. inplace
append is not possible. hence you need to assign the result a dataframe object if you want to use it later. ignore_index
can be used to ignore the index of the dataframe that is assigned to the target dataframe. df2 = {'First Name': 'Vikram', 'Last Name': 'Aruchamy', 'Country': 'India'}
df = df.append(df2, ignore_index = True)
df
df2
is appended to df
and assigned it back to the df
object. Country | First Name | Last Name | |
---|---|---|---|
0 | Raj | Kumar | India |
1 | India | Vikram | Aruchamy |
2 | Shivam | Pandey | India |
3 | Shivam | Pandey | India |
4 | Krishna | Kumar | India |
5 | None | None | None |
6 | India | Vikram | Aruchamy |
append()
method. pd.DataFrame()
method. append()
method for each row is a costlier operation. But adding the rows to the list is not costlier. Hence, you can add to list and create a dataframe using that list. data = []
data.append(['Krishna', 'Kumar', 'India'])
data.append(['Ram', 'Kumar', 'India'])
data.append(['Shivam', 'Pandey', 'India'])
df = pd.DataFrame(data, columns=['First Name', 'Last Name', 'Country'])
df
First Name | Last Name | Country | |
---|---|---|---|
0 | Krishna | Kumar | India |
1 | Ram | Kumar | India |
2 | Shivam | Pandey | India |
append()
, iloc[]
, loc[]
, concatenating two dataframes using concat()
.