Wednesday, July 3, 2019

IslandT: Further Exploring the Pandas.DataFrame Object method

  1. DataFrame.index
  2. DataFrame.columns
  3. DataFrame.at
  4. DataFrame.iat
  5. DataFrame.loc
  6. DataFrame.iloc

In this article, we will further look at the other methods of the DataFrame object, we will continue to explore the DataFrame object methods in a few more chapters before moving forward to the other Pandas objects.

If we want to know what is the label name for the index and the column of a DataFrame table, we can use the below DataFrame properties.

df.index
df.columns
The index and the column label for the DataFrame table

We can access any data in the DataFrame table with the following methods.

DataFrame.at # provide the [index name, column name] data location
DataFrame.iat # provide the [row index, column index] of the data
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(6, 4), index=[0, 1, 2, 3, 4, 5], columns=['A', 'B', 'C', 'D'])

print(df)
print(df.at[4, 'B'])
print(df.iat[4, 3])
The value of a particular index within the DataFrame table

Besides that, we can also change the data within the table with below command.

df.at[4, 'B'] = 0.05
df.iat[4, 3] = 0.06

The next two methods are the same, we just need an index label to access the data value of the entire column.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(6, 4), index=[0, 1, 2, 3, 4, 5], columns=['A', 'B', 'C', 'D'])

print(df)
print(df.loc[2])
print(df.iloc[4])
The index row result

We can even access a single data by providing a tuple of the index and the column name.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(6, 4), index=[0, 1, 2, 3, 4, 5], columns=['A', 'B', 'C', 'D'])

print(df)
print(df.loc[(2), 'C'])
The tuple of (index, column)

There you have it, we will continue to explore the methods of the DataFrame object in the next chapter of this web analysis project series. If you like this post, don’t forget to subscribe to other rss feeds series as well!



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...