Skip to main content

Tuples in Python

 A tuple is an assortment of items which requested and permanent. Tuples are successions, very much like records. The contrasts among tuples and records are, the tuples can't be changed not normal for records and tuples use enclosures, though records utilize square sections.  Making a tuple is pretty much as straightforward as putting diverse comma-isolated qualities. Alternatively you can put these comma-isolated qualities between enclosures moreover. For instance −  tup1 = ('material science', 'science', 1997, 2000);  tup2 = (1, 2, 3, 4, 5 );  tup3 = "a", "b", "c", "d";  The void tuple is composed as two enclosures containing nothing −  tup1 = ();  To compose a tuple containing a solitary worth you need to incorporate a comma, despite the fact that there is just one worth −  tup1 = (50,);  Like string files, tuple records start at 0, and they can be cut, linked, etc.  Getting to Values in Tuples  To get to values in tuple, u...

Reindexing in Python Pandas

Reindexing is used to change the row labels and column labels of a DataFrame.

It means to conform the data to match a given set of labels along a particular axis.


It helps us to perform Multiple operations through indexing like –

·        To insert a missing value (NaN) markers in label locations where no data for the label existed before.

·        To reorder the existing data to match a new set of labels.

 

Example:

import pandas as pd

import numpy as np

N=20

data = pd.DataFrame({

   'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),

   'x': np.linspace(0,stop=N-1,num=N),

   'y': np.random.rand(N),

   'C': np.random.choice(['Low','Medium','High'],N).tolist(),

   'D': np.random.normal(100, 10, size=(N)).tolist()

})

#reindexing the DataFrame

data_reindexed = data.reindex(index=[0,2,5], columns=['A', 'C', 'B'])

print(data_reindexed)

 

Output:

           A     C   B

0 2016-01-01  High NaN

2 2016-01-03   Low NaN

5 2016-01-06  High NaN

 

 

How to Reindex to Align with Other Objects?

Lets us consider if you want to take an object and reindex its axes and labeled the same as another object.

Take an example to get better understanding

 

Example:

import pandas as pd

import numpy as np

data1 = pd.DataFrame(np.random.randn(10,3),columns=['column1','column2','column3'])

data2 = pd.DataFrame(np.random.randn(7,3),columns=['column1','column2','column3'])

data1 = data1.reindex_like(data2)

print(data1)

 

Output:

column1   column2   column3

0  0.271240  0.201199 -0.151743

1 -0.269379  0.262300  0.019942

2  0.685737 -0.233194 -0.652832

3 -1.416394 -0.587026  1.065789

4 -0.590154 -2.194137  0.707365

5  0.393549  1.801881 -2.529611

6  0.062660 -0.996452 -0.029740

Note − Here, the data1 DataFrameis altered and reindexed like data2. If the column names do not should be matched NaN will be added for the entire column label.


How to Fill values while ReIndexing?

We can also fill the missing value while we are reindexing the dataset.

Pandas reindex() method takes an optionalparameter which helps to fill the values. The parameters are as follows-

·        pad/ffill – It will fill values in the forward direction.

·        bfill/backfill – It will fill the values backward direction.

·        nearest – It will fill the values from the nearest index values.

 

Example

import pandas as pd

import numpy as np

df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])

df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3'])

# Padding NAN's

print(df2.reindex_like(df1))

# Now Fill the NAN's with preceding Values

print ("Data Frame with Forward Fill:")

print (df2.reindex_like(df1,method='ffill'))


Output

       col1      col2      col3

0 -1.046918  0.608691  1.081329

1 -0.396384 -0.176895 -1.896393

2       NaN       NaN       NaN

3       NaN       NaN       NaN

4       NaN       NaN       NaN

5       NaN       NaN       NaN

Data Frame with Forward Fill:

       col1      col2      col3

0 -1.046918  0.608691  1.081329

1 -0.396384 -0.176895 -1.896393

2 -0.396384 -0.176895 -1.896393

3 -0.396384 -0.176895 -1.896393

4 -0.396384 -0.176895 -1.896393

5 -0.396384 -0.176895 -1.896393

 

Note – In the above example the last four rows are padded.

 

How to Limit on Filling values while Reindexing?

Reindex() function also takes a parameter “limit” which is used to maximum count of the consecutive matches.

Let’s understand with an example-

import pandas as pd

import numpy as np

df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])

df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3'])

# Padding NAN's

print(df2.reindex_like(df1))

# Now Fill the NAN's with preceding Values

print ("Data Frame with Forward Fill limiting to 1:")

print(df2.reindex_like(df1,method='ffill',limit=1))

 

Output

       col1      col2      col3

0  0.824697  0.122557 -0.156242

1  0.528174 -1.140847 -1.158778

2       NaN       NaN       NaN

3       NaN       NaN       NaN

4       NaN       NaN       NaN

5       NaN       NaN       NaN

Data Frame with Forward Fill limiting to 1:

       col1      col2      col3

0  0.824697  0.122557 -0.156242

1  0.528174 -1.140847 -1.158778

2  0.528174 -1.140847 -1.158778

3       NaN       NaN       NaN

4       NaN       NaN       NaN

5       NaN       NaN       NaN

 

Note – In the above we can observe that only the 7th row is filled by the preceding 6th row. Then, the rows are left as they are.


How to Rename in Python?

Python provides a rename() method which allows us to relabel an axis based on the same mapping (a dict or a Series) or an arbitrary function.

Let’s take an example to understand

import pandas as pd

import numpy as np

data1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])

print(data1)

print ("After renaming the rows and columns:")

print(data1.rename(columns={'col1' : 'c1', 'col2' : 'c2'},

index = {0 : 'apple', 1 : 'banana', 2 : 'mango'}))


Output

       col1      col2      col3

0  0.047170  0.378306 -1.198150

1  1.183208 -2.195630 -0.798192

2  0.256581  0.627994 -0.674260

3  0.240853  1.677340  1.497613

4  0.820688  0.920151 -1.431485

5 -0.010474 -0.228373 -0.392640

After renaming the rows and columns:

              c1        c2      col3

apple   0.047170  0.378306 -1.198150

banana  1.183208 -2.195630 -0.798192

mango   0.256581  0.627994 -0.674260

3       0.240853  1.677340  1.497613

4       0.820688  0.920151 -1.431485

5      -0.010474 -0.228373 -0.392640

 

This rename() method provides an inplace named parameter, which by default is False and copies the underlying data. Pass inplace=True to rename the data in place.


Insideaiml is one of the best platforms where you can learn Python, Data Science, Machine Learning, Artificial Intelligence & showcase your knowledge to the outside world.


#insideaiml  #python  #pythonpandas  #artificialintelligence  #machinelearning  #datascience  

 

Comments

Popular posts from this blog

Tuples in Python

 A tuple is an assortment of items which requested and permanent. Tuples are successions, very much like records. The contrasts among tuples and records are, the tuples can't be changed not normal for records and tuples use enclosures, though records utilize square sections.  Making a tuple is pretty much as straightforward as putting diverse comma-isolated qualities. Alternatively you can put these comma-isolated qualities between enclosures moreover. For instance −  tup1 = ('material science', 'science', 1997, 2000);  tup2 = (1, 2, 3, 4, 5 );  tup3 = "a", "b", "c", "d";  The void tuple is composed as two enclosures containing nothing −  tup1 = ();  To compose a tuple containing a solitary worth you need to incorporate a comma, despite the fact that there is just one worth −  tup1 = (50,);  Like string files, tuple records start at 0, and they can be cut, linked, etc.  Getting to Values in Tuples  To get to values in tuple, u...

Python Numbers: A Detailed Guide

 You don't be a human calculator to program well. Not many developers need to know more than essential variable-based math. How much numerical you need to know relies upon the application you're chipping away at. As a general rule, the degree of math needed to be a software engineer is lower than you may anticipate. Even though math and PC writing computer programs aren't just about as connected as certain individuals may accept, numbers are a necessary piece of any programming language, and Python is no exemption.  In this exercise, you'll figure out how to:  Make numbers and gliding point numbers  Round numbers to a given number of decimal spots  Organization and show numbers in strings  We should begin!  Note: This instructional exercise is adjusted from the section "Numbers and Math" in Python Basics: A Practical Introduction to Python 3.  The book utilizes Python's implicit IDLE manager to make and alter Python records and interface with the ...

What Is The Use Of Artificial Intelligence In Future?

  Artificial Intelligence is definitely the future of the world. Artificial Intelligence will drive the economy of tomorrow. Are you excited about   Artificial Intelligence?   Google, Facebook, Apple, Microsoft are all moving ahead at great speed in improving this Artificial Intelligence. So, it’s very exciting! Software is going to solve that where it’ll look at the new information and present to you knowing about your interests what would be most valuable. So: making us more efficient. We’re focusing on autonomous systems and we sort of see it has the mother of all AI projects. Areas where Artificial Intelligence is going to impact our future lives. Autonomous Transportation:   As the companies like Uber, Google & General Motors are struggling hard to establish themselves at the top of this market, this would soon bring a complete change to an AI – guided transportation and would become a reality. All of the three from Uber to Google to General Motors all want ...