Pandas DataFrame API Manual
DataFrame is a two-dimensional labeled data structure; you can think of it as an Excel spreadsheet, an SQL table, or a dictionary-like collection.
The following is a manual of commonly used Pandas DataFrame APIs:
DataFrame constructor
| Method | Description |
|---|---|
pd.DataFrame(data, index, columns, dtype, copy) |
Create a DataFrame object, supporting custom data, index, column names, and data types. |
DataFrame attributes
| property | Description |
|---|---|
DataFrame.values |
Return the data portion of the DataFrame (a numpy array). |
DataFrame.index |
Return the row index of the DataFrame. |
DataFrame.columns |
Return the column names of the DataFrame. |
DataFrame.dtypes |
Return the data type of each column. |
DataFrame.shape |
Return the shape of the DataFrame (as a tuple). |
DataFrame.size |
Return the total number of elements in the DataFrame. |
DataFrame.empty |
Check whether the DataFrame is empty. |
DataFrame.ndim |
Return the number of dimensions of the DataFrame (always 2). |
DataFrame.T |
Return the transpose of the DataFrame. |
DataFrame.axes |
Return a list of the row index and column names. |
DataFrame.memory_usage() |
Return the memory usage of each column. |
DataFrame methods
Data viewing
| Method | Description |
|---|---|
DataFrame.head(n=5) |
Return the first n rows of data. |
DataFrame.tail(n=5) |
Return the last n rows of data. |
DataFrame.describe() |
Return a statistical summary of the DataFrame (such as count, mean, standard deviation, etc.). |
DataFrame.info() |
Print a brief summary of the DataFrame (such as column names, data types, number of non-null values, etc.). |
Handling Missing Values
| Method | Description |
|---|---|
DataFrame.isnull() |
Check whether each element is a missing value (NaN). |
DataFrame.notnull() |
Check whether each element is not a missing value. |
DataFrame.dropna() |
Remove rows or columns containing missing values. |
DataFrame.fillna(value) |
Fill missing values with a specified value. |
Data operations
| Method | Description |
|---|---|
DataFrame.drop() |
Delete the specified rows or columns. |
DataFrame.rename() |
Rename the row index or column names. |
DataFrame.set_index() |
Set the specified column as the index. |
DataFrame.reset_index() |
Reset the index. |
DataFrame.sort_values() |
Sort by value. |
DataFrame.sort_index() |
Sort by index. |
DataFrame.replace() |
Replace values in the DataFrame. |
DataFrame.append() |
Append another DataFrame. |
DataFrame.join() |
Join another DataFrame based on the index or columns. |
DataFrame.merge() |
Merge another DataFrame based on the specified column. |
DataFrame.concat() |
Concatenate multiple DataFrames along the specified axis. |
DataFrame.update() |
Update the current DataFrame with values from another DataFrame. |
DataFrame.pivot() |
Create a pivot table. |
DataFrame.melt() |
Convert wide-format data to long-format data. |
Data selection
| Method | Description |
|---|---|
DataFrame.loc[] |
Select data by label. |
DataFrame.iloc[] |
Select data by position. |
DataFrame.at[] |
Select a single value by label. |
DataFrame.iat[] |
Select a single value by position. |
DataFrame.filter() |
Select data by column name. |
DataFrame.get() |
Get the values of the specified column. |
DataFrame.query() |
Query data by condition. |
Data Transformation
| Method | Description |
|---|---|
DataFrame.astype() |
Convert the DataFrame to the specified data type. |
DataFrame.apply() |
Apply a function to the rows or columns of the DataFrame. |
DataFrame.applymap() |
Apply a function to each element of the DataFrame. |
DataFrame.map() |
Apply a function to each element of a Series. |
DataFrame.to_dict() |
Convert the DataFrame to a dictionary. |
DataFrame.to_numpy() |
Convert the DataFrame to a numpy array. |
DataFrame.to_csv() |
Save the DataFrame as a CSV file. |
DataFrame.to_excel() |
Save the DataFrame as an Excel file. |
Statistical computation
| Method | Description |
|---|---|
DataFrame.sum() |
Return the sum of each column. |
DataFrame.mean() |
Return the mean of each column. |
DataFrame.median() |
Return the median of each column. |
DataFrame.min() |
Return the minimum of each column. |
DataFrame.max() |
Return the maximum of each column. |
DataFrame.std() |
Return the standard deviation of each column. |
DataFrame.var() |
Return the variance of each column. |
DataFrame.count() |
Return the number of non-missing values in each column. |
DataFrame.corr() |
Return the correlation coefficient matrix between columns. |
DataFrame.cov() |
Return the covariance matrix between columns. |
DataFrame.mode() |
Return the mode of each column. |
DataFrame.quantile() |
Return the quantiles of each column. |
Time series operations
| Method | Description |
|---|---|
DataFrame.dt |
Access date-time attributes (only applicable to columns of datetime type). |
DataFrame.resample() |
Resample time series data. |
DataFrame.shift() |
Shift data along the time axis. |
String Operations
| Method | Description |
|---|---|
DataFrame.str |
Access string methods (only applicable to columns of string type). |
DataFrame.str.lower() |
Convert strings to lowercase. |
DataFrame.str.upper() |
Convert strings to uppercase. |
DataFrame.str.contains() |
Check whether a string contains a specified pattern. |
Example
Example
import
pandas
as
pd
Create DataFrame
df
=
pd.
DataFrame
(
{
'A'
:
[
1
,
2
,
3
]
,
'B'
:
[
4
,
5
,
6
]
}
,
index
=
[
'a'
,
'b'
,
'c'
]
)
View data
(
df.
head
(
2
)
)
Output the first 2 rows
Handling Missing Values
df_with_nan
=
pd.
DataFrame
(
{
'A'
:
[
1
,
None
,
3
]
,
'B'
:
[
4
,
5
,
None
]
}
)
(
df_with_nan.
fillna
(
0
)
)
Fill missing values with 0
Statistical computation
(
df.
mean
(
)
)
Output the mean of each column
For more detailed information, refer to Pandas official documentation。