Pandas Data Frame
A Pandas DataFrame is a tabular data structure that contains multiple columns of data, with each column being a different type of data. It is similar to a spreadsheet, but it is more powerful and flexible. DataFrames can be used to store and manipulate data from a variety of sources, including CSV files, JSON files, and databases.
To create a DataFrame, you can use the DataFrame() constructor. The constructor takes a variety of arguments, including the data to be stored in the DataFrame, the names of the columns, and the index. For example, the following code creates a DataFrame with two columns, name and age:
Code snippet
import pandas as pd
df = pd.DataFrame({'name': ['John Doe', 'Jane Doe'], 'age': [30, 25]})
Once you have created a DataFrame, you can access the data in a variety of ways. You can use the loc and iloc accessors to access specific rows and columns, or you can use the at and iat accessors to access specific elements. For example, the following code prints the name of the first person in the DataFrame:
print(df.loc[0, 'name'])
Output:
Code snippet
John Doe
You can also use the head() and tail() methods to view the first and last few rows of the DataFrame, respectively. For example, the following code prints the first five rows of the DataFrame:
Code snippet
df.head()
Output:
Code snippet
name age
0 John Doe 30
1 Jane Doe 25
You can use the describe() method to get a summary of the data in the DataFrame. For example, the following code prints a summary of the data in the age column:
Code snippet
df['age'].describe()
Output:
Code snippet
count 2.000000
mean 27.500000
std 2.500000
min 25.000000
25% 25.000000
50% 30.000000
75% 30.000000
max 30.000000
You can use the plot() method to plot the data in the DataFrame. For example, the following code plots the age column:
Code snippet
df['age'].plot()
For more information on Pandas DataFrames, please visit the Pandas documentation: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html
Comments
Post a Comment