Pandas: Add Two Columns into a New Column in Dataframe

Methods to add two columns into a new column in Dataframe

In this article, we discuss how to add to column to an existing column in the dataframe and how to add two columns to make a new column in the dataframe using pandas. We will also discuss how to deal with NaN values.

  • Method 1-Sum two columns together to make a new series

In this method, we simply select two-column by their column name and then simply add them.Let see this with the help of an example.

import pandas as pd 
import numpy as np 
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, np.NaN, 81) , 
            ('Abhay', 25,'Rajasthan' , 90) , 
            ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n') 
total = df['Age'] + df['Marks']
print("New Series \n") 
print(total)
print(type(total))

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22        NaN     81
3  Abhay   25  Rajasthan     90
4  Ajjet   21      Delhi     74 

New Series 

0    119
1    118
2    103
3    115
4     95
dtype: int64
<class 'pandas.core.series.Series'>

Here we see that when we add two columns then a series will be formed.]

Note: We can’t add a string with int or float. We can only add a string with a string or a number with a number.

Let see the example of adding string with string.

import pandas as pd 
import numpy as np 
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, 'Kolkata', 81) , 
            ('Abhay', 25,'Rajasthan' , 90) , 
            ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n') 
total = df['Name'] + " "+df['City']
print("New Series \n") 
print(total)
print(type(total))

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22    Kolkata     81
3  Abhay   25  Rajasthan     90
4  Ajjet   21      Delhi     74 

New Series 

0         Raj Mumbai
1        Rahul Delhi
2       Aadi Kolkata
3    Abhay Rajasthan
4        Ajjet Delhi
dtype: object
<class 'pandas.core.series.Series'>
  • Method 2-Sum two columns together having NaN values to make a new series

In the previous method, there is no NaN or missing values but in this case, we also have NaN values. So when we add two columns in which one or two-column contains NaN values then we will see that we also get the result as NaN. Let see this with the help of an example.

import pandas as pd 
import numpy as np 
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, 'Kolkata', np.NaN) , 
            ('Abhay', np.NaN,'Rajasthan' , 90) , 
            ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n') 
total = df['Marks'] + df['Age']
print("New Series \n") 
print(total)
print(type(total))

Output

Original Dataframe

    Name   Age       City  Marks
0    Raj  24.0     Mumbai   95.0
1  Rahul  21.0      Delhi   97.0
2   Aadi  22.0    Kolkata    NaN
3  Abhay   NaN  Rajasthan   90.0
4  Ajjet  21.0      Delhi   74.0 

New Series 

0    119.0
1    118.0
2      NaN
3      NaN
4     95.0
dtype: float64
<class 'pandas.core.series.Series'>
  • Method 3-Add two columns to make a new column

We know that a dataframe is a group of series. We see that when we add two columns it gives us a series and we store that sum in a variable. If we make that variable a column in the dataframe then our work will be easily done. Let see this with the help of an example.

import pandas as pd 
import numpy as np 
students = [('Raj', 24, 'Mumbai', 95) , 
('Rahul', 21, 'Delhi' , 97) , 
('Aadi', 22, 'Kolkata',76) , 
('Abhay',23,'Rajasthan' , 90) , 
('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n') 
df['total'] = df['Marks'] + df['Age']
print("New Dataframe \n") 
print(df)
 
print(df)

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22    Kolkata     76
3  Abhay   23  Rajasthan     90
4  Ajjet   21      Delhi     74 

New Dataframe 

    Name  Age       City  Marks  total
0    Raj   24     Mumbai     95    119
1  Rahul   21      Delhi     97    118
2   Aadi   22    Kolkata     76     98
3  Abhay   23  Rajasthan     90    113
4  Ajjet   21      Delhi     74     95
  • Method 4-Add two columns with NaN values to make a new column

The same is the case with NaN values. But here NaN values will be shown.Let see this with the help of an example.

import pandas as pd 
import numpy as np 
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, 'Kolkata', np.NaN) , 
            ('Abhay', np.NaN,'Rajasthan' , 90) , 
            ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n') 
df['total'] = df['Marks'] + df['Age']
print("New Dataframe \n") 
print(df)

Output

Original Dataframe

    Name   Age       City  Marks
0    Raj  24.0     Mumbai   95.0
1  Rahul  21.0      Delhi   97.0
2   Aadi  22.0    Kolkata    NaN
3  Abhay   NaN  Rajasthan   90.0
4  Ajjet  21.0      Delhi   74.0 

New Dataframe 

    Name   Age       City  Marks  total
0    Raj  24.0     Mumbai   95.0  119.0
1  Rahul  21.0      Delhi   97.0  118.0
2   Aadi  22.0    Kolkata    NaN    NaN
3  Abhay   NaN  Rajasthan   90.0    NaN
4  Ajjet  21.0      Delhi   74.0   95.0

So these are the methods to add two columns in the dataframe.

Matplotlib: Line plot with markers

Methods to draw line plot with markers with the help of Matplotlib

In this article, we will discuss some basics of matplotlib and then discuss how to draw line plots with markers.

Matplotlib

We know that data that is in the form of numbers is difficult and boring to analyze. But if we convert that number into graphs, bar plots, piecharts, etc then it will be easy and interesting to visualize the data. Here Matplotlib library of python came into use. Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.

For using this library we have to first import it into the program. For importing this we can use

from matplotlib import pyplot as plt or import matplotlib. pyplot as plt.

In this article, we only discuss the line plot. So let see the function in matplotlib to draw a line plot.

syntax:  plt.plot(x,y, scalex=True, scaley=True, data=None, marker=’marker style’, **kwargs)

Parameters

  1. x,y: They represent vertical and horizontal axis.
  2. scalex, scaley: These parameters determine if the view limits are adapted to the data limits. The default value is True.
  3. marker: It contains types of markers that can be used. Like point marker, circle marker, etc.

Here is the list of markers used in this

  • “’.’“           point marker
  • “’,’“           pixel marker
  • “’o’“          circle marker
  • “’v’“          triangle_down marker
  • “’^’“          triangle_up marker
  • “'<‘“          triangle_left marker
  • “’>’“          triangle_right marker
  • “’1’“          tri_down marker
  • “’2’“          tri_up marker
  • “’3’“          tri_left marker
  • “’4’“          tri_right marker
  • “’s’“          square marker
  • “’p’“          pentagon marker
  • “’*’“          star marker
  • “’h’“          hexagon1 marker
  • “’H’“         hexagon2 marker
  • “’+’“          plus marker
  • “’x’“          x marker
  • “’D’“         diamond marker
  • “’d’“          thin_diamond marker
  • “’|’“           vline marker
  • “’_’“          hline marker

Examples of Line plot with markers in matplotlib

  • Line Plot with the Point marker

Here we use marker='.'.Let see this with the help of an example.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-5,40,.5)
y = np.sin(x)
plt.plot(x,y, marker='.')
plt.title('Sin Function')
plt.xlabel('x values')
plt.ylabel('y= sin(x)')
plt.show()

Output

  • Line Plot with the Point marker and give marker some color

In the above example, we see the color of the marker is the same as the color of the line plot. So there is an attribute in plt.plot() function marker face color or mfc: color which is used to give color to the marker. Let see this with the help of an example.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-5,40,.5)
y = np.sin(x)
plt.plot(x,y, marker='.',mfc='red')
plt.title('Sin Function')
plt.xlabel('x values')
plt.ylabel('y= sin(x)')
plt.show()

Output

Here we see that color of the pointer changes to red.

  • Line Plot with the Point marker and change the size of the marker

To change the size of the marker there is an attribute in pointer ply.plot() function that is used to achieve this. marker size or ms attribute is used to achieve this. We can pass an int value in ms and then its size increases or decreases according to this. Let see this with the help of an example.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-5,40,.5)
y = np.sin(x)
plt.plot(x,y, marker='.',mfc='red',ms='17')
plt.title('Sin Function')
plt.xlabel('x values')
plt.ylabel('y= sin(x)')
plt.show()

Output

Here we see that size of the pointer changes.

  • Line Plot with the Point marker and change the color of the edge of the marker

We can also change the color of the edge of marker with the help of markeredgecolor or mec attribute. Let see this with the help of an example.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-5,40,.5)
y = np.sin(x)
plt.plot(x,y, marker='.',mfc='red',ms='17', mec='yellow')
plt.title('Sin Function')
plt.xlabel('x values')
plt.ylabel('y= sin(x)')
plt.show()

Output

Here we see that the color of the edge of the pointer changes to yellow.

So here are some examples of how we can work with markers in line plots.

Note: These examples are applicable to any of the marker.

Pandas: Dataframe.fillna()

Dataframe.fillna() in Dataframes using Python

In this article, we will discuss how to use Dataframe.fillna() method with examples, like how to replace NaN values in a complete dataframe or some specific rows/columns

Dataframe.fillna()

Dataframe.fillna() is used to fill NaN values with some other values in Dataframe. This method widely came into use when there are fewer NaN values in any column so instead of dropping the whole column we replace the NaN or missing values of that column with some other values.

Syntax: DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)

Parameters

1) Value: This parameter contains the values that we want to fill instead of NaN values. By default value is None.

2) method: The method parameter is used when the value doesn’t pass. There are different methods like backfill,bfill, etc. By default method is None.

3) axis: axis=1 means fill NaN values in columns and axis=0 means fill NaN values in rows.

4) inplace: It is a boolean which makes the changes in dataframe itself if True.

Different methods to use Dataframe.fillna() method

  • Method 1: Replace all NaN values in Dataframe

In this method, we normally pass some value in the value parameter and all the NaN values will be replaced with that value. Let see this with the help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 95) ,
            ('Rahul', np.NaN,97) ,
            ('Aadi', 22,81) ,
            ('Abhay', np.NaN,np.NaN) ,
            ('Ajjet', 21,74),
            ('Amar',np.NaN,np.NaN),
            ('Aman',np.NaN,76)]
# Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns=['Name', 'Age','Marks'])
print("Original Dataframe\n")
print(df,'\n')
new_df=df.fillna(0)
print("New Dataframe\n")
print(new_df)

Output

Original Dataframe

    Name   Age  Marks
0    Raj  24.0   95.0
1  Rahul   NaN   97.0
2   Aadi  22.0   81.0
3  Abhay   NaN    NaN
4  Ajjet  21.0   74.0
5   Amar   NaN    NaN
6   Aman   NaN   76.0 

New Dataframe

    Name   Age  Marks
0    Raj  24.0   95.0
1  Rahul   0.0   97.0
2   Aadi  22.0   81.0
3  Abhay   0.0    0.0
4  Ajjet  21.0   74.0
5   Amar   0.0    0.0
6   Aman   0.0   76.0

Here we see that we replace all NaN values with 0.

  • Method 2- Replace all NaN values in specific columns

In this method, we replace all NaN values with some other values but only in specific columns not on the whole dataframe.

import pandas as pd
import numpy as np
students = [('Raj', 24, 95) ,
            ('Rahul', np.NaN,97) ,
            ('Aadi', 22,81) ,
            ('Abhay', np.NaN,np.NaN) ,
            ('Ajjet', 21,74),
            ('Amar',np.NaN,np.NaN),
            ('Aman',np.NaN,76)]
# Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns=['Name', 'Age','Marks'])
print("Original Dataframe\n")
print(df,'\n')
df['Age'].fillna(0,inplace=True)
print("New Dataframe\n")
print(df)

Output

Original Dataframe

    Name   Age  Marks
0    Raj  24.0   95.0
1  Rahul   NaN   97.0
2   Aadi  22.0   81.0
3  Abhay   NaN    NaN
4  Ajjet  21.0   74.0
5   Amar   NaN    NaN
6   Aman   NaN   76.0 

New Dataframe

    Name   Age  Marks
0    Raj  24.0   95.0
1  Rahul   0.0   97.0
2   Aadi  22.0   81.0
3  Abhay   0.0    NaN
4  Ajjet  21.0   74.0
5   Amar   0.0    NaN
6   Aman   0.0   76.0

Here we see that the NaN value only in the Age column replaces with 0. Here we use inplace=’true’ because we want changes to be made in the original dataframe.

  • Method 3- Replace NaN values of one column with values of other columns

Here we pass the column in the value parameter of which we want the value to be copied.Let see this with help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 95) ,
            ('Rahul', np.NaN,97) ,
            ('Aadi', 22,81) ,
            ('Abhay', np.NaN,87) ,
            ('Ajjet', 21,74),
            ('Amar',np.NaN,76),
            ('Aman',np.NaN,76)]
# Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns=['Name', 'Age','Marks'])
print("Original Dataframe\n")
print(df,'\n')
df['Age'].fillna(value=df['Marks'],inplace=True)
print("New Dataframe\n")
print(df)

Output

Original Dataframe

    Name   Age  Marks
0    Raj  24.0     95
1  Rahul   NaN     97
2   Aadi  22.0     81
3  Abhay   NaN     87
4  Ajjet  21.0     74
5   Amar   NaN     76
6   Aman   NaN     76 

New Dataframe

    Name   Age  Marks
0    Raj  24.0     95
1  Rahul  97.0     97
2   Aadi  22.0     81
3  Abhay  87.0     87
4  Ajjet  21.0     74
5   Amar  76.0     76
6   Aman  76.0     76

Here we see NaN values of the Age column are replaced with non NaN value of the Marks Column.

  • Method 4-Replace NaN values in specific rows

To replace NaN values in a row we need to use .loc[‘index name’] to access a row in a dataframe, then we will call the fillna() function on that row. Let see this with help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 95) ,
            ('Rahul', np.NaN,97) ,
            ('Aadi', 22,81) ,
            ('Abhay', np.NaN,87) ,
            ('Ajjet', 21,74),
            ('Amar',np.NaN,76),
            ('Aman',np.NaN,76)]
# Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns=['Name', 'Age','Marks'])
print("Original Dataframe\n")
print(df,'\n')
df.loc[1]=df.loc[1].fillna(value=0)
print("New Dataframe\n")
print(df)

Output

Original Dataframe

    Name   Age  Marks
0    Raj  24.0     95
1  Rahul   NaN     97
2   Aadi  22.0     81
3  Abhay   NaN     87
4  Ajjet  21.0     74
5   Amar   NaN     76
6   Aman   NaN     76 

New Dataframe

    Name   Age  Marks
0    Raj  24.0     95
1  Rahul   0.0     97
2   Aadi  22.0     81
3  Abhay   NaN     87
4  Ajjet  21.0     74
5   Amar   NaN     76
6   Aman   NaN     76

So these are some of the ways to use Dataframe.fillna().

Get Rows And Columns Names In Dataframe Using Python

Methods to get rows and columns names in dataframe

In this we will study different methods to get rows and column names in a dataframe.

Methods to get column name in dataframe

  • Method 1: By iterating over columns

In this method, we will simply be iterating over all the columns and print the names of each column. Point to remember that dataframe_name. columns give a list of columns.Let see this with the help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, 'Kolkata', 81) , 
            ('Abhay', 24,'Rajasthan' ,76) , 
              ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n')
print(df.columns,'\n')
print("columns are:")
for column in df.columns:
  print(column,end=" ")

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22    Kolkata     81
3  Abhay   24  Rajasthan     76
4  Ajjet   21      Delhi     74 

Index(['Name', 'Age', 'City', 'Marks'], dtype='object') 

columns are:
Name Age City Marks 

Here we see that df. columns give a list of columns and by iterating over this list we can easily get column names.

  • Method 2-Using columns.values

columns. values return an array of column names. Let see this with the help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, 'Kolkata', 81) , 
            ('Abhay', 24,'Rajasthan' ,76) , 
              ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n')
print("columns are:")
print(df.columns.values,'\n')

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22    Kolkata     81
3  Abhay   24  Rajasthan     76
4  Ajjet   21      Delhi     74 

columns are:
['Name' 'Age' 'City' 'Marks'] 
  • Method 3- using tolist() method

Using tolist() method with values with given the list of columns. Let see this with the help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, 'Kolkata', 81) , 
            ('Abhay', 24,'Rajasthan' ,76) , 
              ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n')
print("columns are:")
print(df.columns.values.tolist(),'\n')

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22    Kolkata     81
3  Abhay   24  Rajasthan     76
4  Ajjet   21      Delhi     74 

columns are:
['Name', 'Age', 'City', 'Marks'] 
  • Method 4- Access specific column name using index

As we know that columns. values give an array of columns and we can access array elements using an index. So in this method, we use this concept. Let see this with the help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, 'Kolkata', 81) , 
            ('Abhay', 24,'Rajasthan' ,76) , 
              ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n')
print("columns at second index:")
print(df.columns.values[2],'\n')

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22    Kolkata     81
3  Abhay   24  Rajasthan     76
4  Ajjet   21      Delhi     74 

columns at second index:
City 

So these are the methods to get column names.

Method to get rows name in dataframe

  • Method 1-Using index.values

As columns., values give a list or array of columns similarly index. values give a list of array of indexes. Let see this with the help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, 'Kolkata', 81) , 
            ('Abhay', 24,'Rajasthan' ,76) , 
              ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n')
print("Rows are:")
print(df.index.values,'\n')

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22    Kolkata     81
3  Abhay   24  Rajasthan     76
4  Ajjet   21      Delhi     74 

Rows are:
[0 1 2 3 4] 
  • Method 2- Get Row name at a specific index

As we know that index. values give an array of indexes and we can access array elements using an index. So in this method, we use this concept. Let see this with the help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 'Mumbai', 95) , 
('Rahul', 21, 'Delhi' , 97) , 
('Aadi', 22, 'Kolkata', 81) , 
('Abhay', 24,'Rajasthan' ,76) , 
('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n')
print("Row at index 2:")
print(df.index.values[2],'\n')

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22    Kolkata     81
3  Abhay   24  Rajasthan     76
4  Ajjet   21      Delhi     74 

Row at index 2:
2 
  • Method 3-By iterating over indices

As dataframe_names.columns give a list of columns similarly dataframe_name.index gives the list of indexes. Hence we can simply be iterating over all lists of indexes and print rows names. Let see this with help of an example.

import pandas as pd
import numpy as np
students = [('Raj', 24, 'Mumbai', 95) , 
            ('Rahul', 21, 'Delhi' , 97) , 
            ('Aadi', 22, 'Kolkata', 81) , 
            ('Abhay', 24,'Rajasthan' ,76) , 
              ('Ajjet', 21, 'Delhi' , 74)] 
# Create a DataFrame object 
df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) 
print("Original Dataframe\n") 
print(df,'\n')
print("List of indexes:")
print(df.index,'\n')
print("Indexes or rows names are:")
for row in df.index:
  print(row,end=" ")

Output

Original Dataframe

    Name  Age       City  Marks
0    Raj   24     Mumbai     95
1  Rahul   21      Delhi     97
2   Aadi   22    Kolkata     81
3  Abhay   24  Rajasthan     76
4  Ajjet   21      Delhi     74 

List of indexes:
RangeIndex(start=0, stop=5, step=1) 

Indexes or rows names are:
0 1 2 3 4 

So these are the methods to get rows and column names in the dataframe using python.

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Select items from a Dataframe

Pandas: Create Dataframe from List of Dictionaries

Methods of creating a dataframe from a list of dictionaries

In this article, we discuss different methods by which we can create a dataframe from a list of dictionaries. Before going to the actual article let us done some observations that help to understand the concept easily. Suppose we have a list of dictionary:-

list_of_dict = [
{'Name': 'Mayank' , 'Age': 25, 'Marks': 91},
{'Name': 'Raj', 'Age': 21, 'Marks': 97},
{'Name': 'Rahul', 'Age': 23, 'Marks': 79},
{'Name': 'Manish' , 'Age': 23},
]

Here we know that dictionaries consist of key-value pairs. So we can analyze that if we make the key as our column name and values as the column value then a dataframe is easily created. And we have a list of dictionaries so a dataframe with multiple rows also.

pandas.DataFrame

This methods helps us to create dataframe in python

syntax: pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

Let us see different methods to create dataframe from a list of dictionaries

  • Method 1-Create Dataframe from list of dictionaries with default indexes

As we see in in pandas.Datframe() method there is parameter name data.We have to simply pass our list of dictionaries in this method and it will return the dataframe.Let see this with the help of an example.

import pandas as pd
import numpy as np

list_of_dict = [
    {'Name': 'Mayank' ,  'Age': 25,  'Marks': 91},
    {'Name': 'Raj',  'Age': 21,  'Marks': 97},
    {'Name': 'Rahul',  'Age': 23,  'Marks': 79},
    {'Name': 'Manish' ,  'Age': 23,  'Marks': 86},
]
#create dataframe
df=pd.DataFrame(list_of_dict)
print(df)

Output

   Age  Marks    Name
0   25     91  Mayank
1   21     97     Raj
2   23     79   Rahul
3   23     86  Manish

Here we see that dataframe is created with default indexes 0,1,2,3….

Now a question may arise if from any dictionary key-value pair is less than other dictionaries.So in this case what happened.Let understand it with the help of an example.

import pandas as pd
import numpy as np

list_of_dict = [
    {'Name': 'Mayank' ,  'Age': 25,  'Marks': 91},
    {'Name': 'Raj',  'Age': 21,  'Marks': 97},
    {'Name': 'Rahul',  'Marks': 79},
    {'Name': 'Manish' ,  'Age': 23},
]
#create dataframe
df=pd.DataFrame(list_of_dict)
print(df)

Output

    Age  Marks    Name
0  25.0   91.0  Mayank
1  21.0   97.0     Raj
2   NaN   79.0   Rahul
3  23.0    NaN  Manish

Here we see in case of missing key value pair NaN value is there in the output.

  • Method 2- Create Dataframe from list of dictionary with custom indexes

Unlike the previous method where we have default indexes we can also give custom indexes by passes list of indexes in index parameter of pandas.DataFrame() function.Let see this with the help of an example.

import pandas as pd
import numpy as np

list_of_dict = [
    {'Name': 'Mayank' ,  'Age': 25,  'Marks': 91},
    {'Name': 'Raj',  'Age': 21,  'Marks': 97},
    {'Name': 'Rahul',  'Marks': 79},
    {'Name': 'Manish' ,  'Age': 23},
]
#create dataframe
df=pd.DataFrame(list_of_dict,index=['a','b','c','d'])
print(df)

Output

    Age  Marks    Name
a  25.0   91.0  Mayank
b  21.0   97.0     Raj
c   NaN   79.0   Rahul
d  23.0    NaN  Manish

Here we see that instead of default index 1,2,3….. we have now indes a,b,c,d.

  • Method 3-Create Dataframe from list of dictionaries with changed order of columns

With the help of pandas.DataFrame() method we can easily arrange order of column by simply passes list ozf columns in columns parameter in the order in which we want to display it in our dataframe.Let see this with the help of example.

import pandas as pd
import numpy as np

list_of_dict = [
    {'Name': 'Mayank' ,  'Age': 25,  'Marks': 91},
    {'Name': 'Raj',  'Age': 21,  'Marks': 97},
    {'Name': 'Rahul',  'Age': 23,  'Marks': 79},
    {'Name': 'Manish' ,  'Age': 23,  'Marks': 86},
]
#create dataframe
df=pd.DataFrame(list_of_dict,columns=['Name', 'Marks', 'Age'])
print(df)

Output

     Name  Marks  Age
0  Mayank     91   25
1     Raj     97   21
2   Rahul     79   23
3  Manish     86   23

Here also a question may arise if we pass less column in columns parameter or we pass more column in parameter then what happened.Let see this with the help of an example.

Case 1: Less column in column parameter

In this case the column which we don’t pass will be drop from the dataframe.Let see this with the help of an example.

import pandas as pd
import numpy as np

list_of_dict = [
    {'Name': 'Mayank' ,  'Age': 25,  'Marks': 91},
    {'Name': 'Raj',  'Age': 21,  'Marks': 97},
    {'Name': 'Rahul',  'Age': 23,  'Marks': 79},
    {'Name': 'Manish' ,  'Age': 23,  'Marks': 86},
]
#create dataframe
df=pd.DataFrame(list_of_dict,columns=['Name', 'Marks'])
print(df)

Output

     Name  Marks
0  Mayank     91
1     Raj     97
2   Rahul     79
3  Manish     86

Here we see that we didn’t pass Age column that’s why Age clumn is also not in our dataframe.

Case 2: More column in column parameter

In this case a new column will be added in dataframe but its all the value will be NaN.Let see this with the help of an example.

import pandas as pd
import numpy as np

list_of_dict = [
    {'Name': 'Mayank' ,  'Age': 25,  'Marks': 91},
    {'Name': 'Raj',  'Age': 21,  'Marks': 97},
    {'Name': 'Rahul',  'Age': 23,  'Marks': 79},
    {'Name': 'Manish' ,  'Age': 23,  'Marks': 86},
]
#create dataframe
df=pd.DataFrame(list_of_dict,columns=['Name', 'Marks', 'Age','city'])
print(df)

Output

     Name  Marks  Age  city
0  Mayank     91   25   NaN
1     Raj     97   21   NaN
2   Rahul     79   23   NaN
3  Manish     86   23   NaN

So these are the methods to create dataframe from list of dictionary in pandas.

C++ Program to Check Whether a Number is Armstrong or Not

C++ Program to Check Whether a Number is Armstrong or Not

In the previous article, we have discussed about C++ Program to Find LCM of Two Numbers. Let us learn how to Check Whether a Number is Armstrong or Not in C++ Program.

Method to Check Whether a Number is Armstrong or Not in C++

In this article, we discuss different methods to check whether a number is Armstrong or not. The methods that we will discuss are given below.

First, understand what an Armstrong number is. A positive integer is called an Armstrong number (of order n) where n is the number of digits in the number if abcd…….=a^n+b^n+c^n+d^n………  For example, suppose our number is 153 as it has 3 digits so it has order 3.Also 153=1^3+5^3+3^3. Hence 153 is an Armstrong number.

Let discuss different methods to check whether a number is Armstrong or not.

Method 1-Using loop and arithmetic operator

Here idea is to extract the digit of a number using modulo operator and after that add nth power of that digit in a result. We repeat this step until our number becomes 0. If the result is equal to the given number then the number is an Armstrong number else the number is not Armstrong. Let’s write code for this.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int originalNum=153,temp,temp1,rem, result = 0,n;
    temp=originalNum;
    temp1=originalNum;
    
    // count number of digit in a number
     while (originalNum != 0) {
        originalNum /= 10;
        n++;
    }
    
    while (temp1!= 0) {
        rem = temp1% 10;
        result += pow(rem,n);
        temp1 /= 10;
    }

    if (result == temp)
        cout << temp << " is an Armstrong number.";
    else
        cout << temp << " is not an Armstrong number.";

    return 0;
}

Output

153 is an Armstrong number.

Method 2-Converting number to string

Here the step will be the same as method 1 but here the difference is that we will convert the number to a string. Let’s write code for this.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int originalNum=153,rem, result = 0,n,i;
    string str=to_string(originalNum);
    n=str.size();
    for(i=0;i<n;i++)
    {
        string s(1, str[i]);
        result+=pow(stoi(s),n);
    }
    if (result == originalNum)
        cout << originalNum << " is an Armstrong number.";
    else
        cout << originalNum << " is not an Armstrong number.";

    return 0;
}

Output

153 is an Armstrong number.

So these are the methods to check whether a number is an Armstrong number or not in c++.

C++ Program to Print ASCII Value of a Character

C++ Program to Print ASCII Value of a Character

In the previous article, we have discussed about C++ Program to Find GCD. Let us learn how to Print ASCII Value of a Character in C++ Program.

Program to Print ASCII Value of a Character in C++

In this article, we will write a program of how we can print the ASCII value of a character in c++ language.

First of all, let see what an ASCII value is.ASCII is the acronym for the American Standard Code for Information Interchange. It is a code for representing 128 English characters as numbers, with each letter assigned a number from 0 to 127. For example, the ASCII code for uppercase M is 77.

Method 1-Using typecasting

This is the approach for getting the ASCII value of a character. When we typecasting string into int then we get the ASCII value of that character. Let’s write the code for this.

#include <iostream>
using namespace std;

int main() {
    char c = 'a';
    cout << "The ASCII value of " << c << " is " << int(c);
    return 0;
}

Output

The ASCII value of a is 97

By this method, we can get the ASCII value of any character.

C++ Program to Iterate Over a Set

C++ Program to Iterate Over a Set

Methods to iterate over a set in c++

In this article, we discuss the different methods to iterate over a set in c++. The method that we discuss is given below.

Before going to the methods let’s get a brief understanding of the set in c++. Set in C++ is similar to that of set that we studied in mathematics. Here sets are a type of associative containers in which each element has to be unique. It means suppose if we add 50 two times in a set then it will consider 50 as a single element.

Now let’s understand these methods one by one.

Method 1- Iterating set in forward direction using iterator

In this method, we use two iterators to iterate over a set in c++. Out of these two iterators one iterator pointing to the first element and the other iterator pointing next to the last element. So now we know the position of first and last elements so we now can easily use for or while loop to iterate over the set. Let’s get some insight about the iterators that we are going to use.

  1. set::begin() begin() function is used to return an iterator pointing to the first element of the set container.
  2. set::end() It returns an iterator pointing past the last element of the set container.

Now let’s write the code to iterate over set in the forward direction.

#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int>st{10,20,30,40,50,50};
    set<int>::iterator it;
    for(it=st.begin();it!=st.end();it++)
    {
        cout<<*it<<" ";
    }
    return 0;
}

Output

10 20 30 40 50

Here we also see that we store 50 2 times in a set but while iterating we get 50 1 times so it show that set only store unique elements.

Method 2-Iterating set in reverse direction using reverse iterator

In this method, we use two iterators to iterate over a set in the reverse direction in c++. Out of these two iterators one iterator pointing to the right of the first element and the other iterator pointing to the last element. So now we know the position of first and last elements so we now can easily use for or while loop to iterate over the set. Let’s get some insight about the iterators that we are going to use.

  1. set::rbegin() It returns a reverse iterator pointing to the last element in the container.
  2. set::rend() It returns a reverse iterator pointing to the theoretical element right before the first element in the set container.

Now let’s write the code to iterate over set in the reverse direction.

#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int>st{10,20,30,40,50,50};
    set<int>::reverse_iterator it;
    for(it=st.rbegin();it!=st.rend();it++)
    {
        cout<<*it<<" ";
    }
    return 0;
}

Output

50 40 30 20 10

Method 3:- Iterating over set using for each loop

In this method, we will use for each loop to iterate over a set in c++. Here we use auto keyword. The auto keyword specifies that the type of the variable that is being declared will be automatically deducted from its initializer. Let’s write the code for it.

#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int>st{10,20,30,40,50,50};
    
    for(auto it:st)
    {
        cout<<it<<" ";
    }
    return 0;
}

Output

10 20 30 40 50

So these are the methods to iterate over set in c++.

C++ Program to Find of Size of Datatypes

C++ Program to Find of Size of Datatypes

In the previous article, we have discussed about C++ Program to Display Factors of a Number. Let us learn how to find Size of Datatypes in C++ Program.

Method to find the size of datatypes in c++

In this article, we will discuss how we can find the size of data types in c++. The method that we will discuss today is given below.

Let’s first discuss what a datatype is. All variables use data-type during declaration to restrict the type of data to be stored. For example, if we want a variable should store an integer so we gave this variable datatype int. Therefore, we can say that data types are used to tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared. Every data type requires a different amount of memory. This memory requirement is known as the size of the datatype. Let’s discuss the method of how we can get the size of datatype in c++.

Method 1-Using sizeof operator

Sizeof the operator in c++ is used to calculate the size of the variable in c++. When sizeof() is used with the data types such as int, float, char… etc it simply returns the amount of memory is allocated to that data type.

syntax:

sizeof(dataType);

Let’s discuss it with the help of examples.

#include <iostream>
using namespace std;

int main() {
    int integerType; 
    char charType; 
    float floatType; 
    double doubleType; 
  
    cout << "Size of int is: " << 
    sizeof(integerType) <<"\n"; 
  
    cout << "Size of char is: " << 
    sizeof(charType) <<"\n"; 
      
    cout << "Size of float is: " << 
    sizeof(floatType) <<"\n";
  
    cout << "Size of double is: " << 
    sizeof(doubleType) <<"\n"; 
    
    return 0;
}

Output

Size of int is: 4
Size of char is: 1
Size of float is: 4
Size of double is: 8

Note: sizeof() may give different output according to the machine.

C++ Program to Calculate Sum of First N Natural Numbers

C++ Program to Calculate Sum of First N Natural Numbers

In the previous article, we have discussed about C++ Program to Check Whether Number is Even or Odd. Let us learn how to Calculate Sum of First N Natural Numbers in C++ Program.

Methods to calculate the sum of first n natural number numbers in c++

In this article, we see how we can calculate the sum of first n natural numbers. There are different methods to achieve this. Let see all the methods one by one.

Method 1-Using for loop

This is one of the methods to achieve this task. We will start a loop from 1 to the given number and add them in a variable and return or print the variable. Let write the code for this.

#include <iostream>
using namespace std;

int main() {
    int n=10,i,sum=0;
    for(i=0;i<=n;i++)
    {
        sum+=i;
    }
    cout<<sum<<endl;
    return 0;
}

Output

55

Here we find the sum of the first 10 natural numbers and we get the result 55.

Method 2-Using while loop

Instead of using for loop, we can also achieve the same through while loop. The concept will remain same as method 1. Let write the code for this.

#include <iostream>
using namespace std;

int main() {
    int n=10,i=1,sum=0;
    while(i<=n)
    {
        sum=sum+i;
        i++;
    }
    cout<<sum<<endl;
    return 0;
}

Output

55

Method 3-Using sum of AP formula

Here we can analyze that instead of using a loop we can also use the concept of Arithmetic Progression(A.P.). Here the common difference will be 1 and we also know the first term which is 1 and the last term which is n so we simply use the A.P. sum formula.

Formula:-n/2(a + l)

Where n is the total terms, a is the first term and l is the last term.

Let’s write the code for this

#include <iostream>
using namespace std;

int main() {
    int n=10,a=1,sum=0,l=10;
    sum=(n*(a+l))/2;
    cout<<sum<<endl;
    return 0;
}

Output

55

So these are the methods to calculate the sum of first n natural numbers in c++.