banner



How To Keep Columns In Pandas

A Data frame is a two-dimensional information structure, i.due east., data is aligned in a tabular fashion in rows and columns. We can perform basic operations on rows/columns like selecting, deleting, adding, and renaming. In this article, we are using nba.csv file.

Dealing with Columns

In social club to deal with columns, nosotros perform basic operations on columns similar selecting, deleting, calculation and renaming.

Column Selection:
In Order to select a column in Pandas DataFrame, nosotros tin can either access the columns by calling them past their columns proper noun.

import pandas as pd

data = { 'Name' :[ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],

'Historic period' :[ 27 , 24 , 22 , 32 ],

'Address' :[ 'Delhi' , 'Kanpur' , 'Allahabad' , 'Kannauj' ],

'Qualification' :[ 'Msc' , 'MA' , 'MCA' , 'Phd' ]}

df = pd.DataFrame(data)

print (df[[ 'Name' , 'Qualification' ]])

import pandas as pd

information = { 'Proper noun' : [ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],

'Tiptop' : [ 5.one , half-dozen.2 , 5.1 , 5.2 ],

'Qualification' : [ 'Msc' , 'MA' , 'Msc' , 'Msc' ]}

df = pd.DataFrame(data)

address = [ 'Delhi' , 'Bangalore' , 'Chennai' , 'Patna' ]

df[ 'Address' ] = address

print (df)

import pandas as pd

data = pd.read_csv( "nba.csv" , index_col = "Name" )

information.drop([ "Team" , "Weight" ], axis = one , inplace = True )

print (data)

Output:
As shown in the output images, the new output doesn't take the passed columns. Those values were dropped since axis was set equal to ane and the changes were made in the original data frame since inplace was True.

Data Frame earlier Dropping Columns-

Data Frame after Dropping Columns-

For more examples refer to Delete columns from DataFrame using Pandas.drop()

Dealing with Rows:

In order to deal with rows, we can perform basic operations on rows like selecting, deleting, calculation and renaming.

Row Pick:
Pandas provide a unique method to recollect rows from a Data frame.DataFrame.loc[] method is used to retrieve rows from Pandas DataFrame. Rows can likewise exist selected by passing integer location to an iloc[] function.

import pandas as pd

data = pd.read_csv( "nba.csv" , index_col = "Name" )

first = data.loc[ "Avery Bradley" ]

second = information.loc[ "R.J. Hunter" ]

impress (first, "\northward\n\due north" , second)

Output:
As shown in the output image, ii series were returned since there was only one parameter both of the times.

For more than examples refer to Pandas Extracting rows using .loc[]

Row Addition:
In Order to add a Row in Pandas DataFrame, we tin can concat the sometime dataframe with new one.

import pandas as pd

df = pd.read_csv( "nba.csv" , index_col = "Proper noun" )

df.head( 10 )

new_row = pd.DataFrame({ 'Proper noun' : 'Geeks' , 'Squad' : 'Boston' , 'Number' : 3 ,

'Position' : 'PG' , 'Age' : 33 , 'Acme' : '6-2' ,

'Weight' : 189 , 'College' : 'MIT' , 'Salary' : 99999 },

index = [ 0 ])

df = pd.concat([new_row, df]).reset_index(drop = True )

df.head( 5 )

Output:

Data Frame before Calculation Row-

Data Frame afterward Adding Row-

For more examples refer to Add a row at tiptop in pandas DataFrame

Row Deletion:
In Order to delete a row in Pandas DataFrame, we tin can use the drib() method. Rows is deleted by dropping Rows past index label.

import pandas every bit pd

data = pd.read_csv( "nba.csv" , index_col = "Proper noun" )

information.drop([ "Avery Bradley" , "John Holland" , "R.J. Hunter" ,

"R.J. Hunter" ], inplace = Truthful )

information

Output:
As shown in the output images, the new output doesn't have the passed values. Those values were dropped and the changes were made in the original data frame since inplace was True.

Data Frame before Dropping values-

Information Frame after Dropping values-

For more than examples refer to Delete rows from DataFrame using Pandas.drop()

Problem related to Columns:

  • How to get cavalcade names in Pandas dataframe
  • How to rename columns in Pandas DataFrame
  • How to drop one or multiple columns in Pandas Dataframe
  • Get unique values from a column in Pandas DataFrame
  • How to lowercase cavalcade names in Pandas dataframe
  • Use majuscule to a column in Pandas dataframe
  • Capitalize first alphabetic character of a cavalcade in Pandas dataframe
  • Get northward-largest values from a particular column in Pandas DataFrame
  • Go n-smallest values from a particular cavalcade in Pandas DataFrame
  • Convert a column to row name/index in Pandas

Problem related to Rows:

  • Apply function to every row in a Pandas DataFrame
  • How to go rows names in Pandas dataframe

Source: https://www.geeksforgeeks.org/dealing-with-rows-and-columns-in-pandas-dataframe/

0 Response to "How To Keep Columns In Pandas"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel