Pandas DataFrame 选择列

Suraj Joshi 2023年1月30日
  1. 使用索引操作从 Pandas DataFrame 中选择列
  2. 使用 DataFrame.drop() 方法从 Pandas DataFrame 中选择列
  3. 使用 DataFrame.filter() 方法从 Pandas DataFrame 中选择列
Pandas DataFrame 选择列

本教程介绍了如何通过索引或使用 DataFrame.drop()DataFrame.filter() 方法从 Pandas DataFrame 中选择列。

我们将使用下面的 DataFrame df 来解释如何从 Pandas DataFrame 中选择列。

import pandas as pd

df = pd.DataFrame(
    {
        "A": [302, 504, 708, 103, 343, 565],
        "B": [100, 300, 400, 200, 400, 700],
        "C": [300, 400, 350, 100, 1000, 400],
        "D": [10, 15, 5, 0, 2, 7],
        "E": [4, 5, 6, 7, 8, 9],
    }
)

print(df)

输出:

     A    B     C   D  E
0  302  100   300  10  4
1  504  300   400  15  5
2  708  400   350   5  6
3  103  200   100   0  7
4  343  400  1000   2  8
5  565  700   400   7  9

使用索引操作从 Pandas DataFrame 中选择列

import pandas as pd

df = pd.DataFrame(
    {
        "A": [302, 504, 708, 103, 343, 565],
        "B": [100, 300, 400, 200, 400, 700],
        "C": [300, 400, 350, 100, 1000, 400],
        "D": [10, 15, 5, 0, 2, 7],
        "E": [4, 5, 6, 7, 8, 9],
    }
)

derived_df = df[["A", "C", "E"]]

print("The initial DataFrame is:")
print(df, "\n")

print("The DataFrame with A,C and E columns is:")
print(derived_df, "\n")

输出:

The initial DataFrame is:
     A    B     C   D  E
0  302  100   300  10  4
1  504  300   400  15  5
2  708  400   350   5  6
3  103  200   100   0  7
4  343  400  1000   2  8
5  565  700   400   7  9 

The DataFrame with A,C and E columns is:
     A     C  E
0  302   300  4
1  504   400  5
2  708   350  6
3  103   100  7
4  343  1000  8
5  565   400  9 

它从 DataFrame df 中选择列 ACE,并将这些列分配到 derived_df DataFrame 中。

使用 DataFrame.drop() 方法从 Pandas DataFrame 中选择列

import pandas as pd

df = pd.DataFrame(
    {
        "A": [302, 504, 708, 103, 343, 565],
        "B": [100, 300, 400, 200, 400, 700],
        "C": [300, 400, 350, 100, 1000, 400],
        "D": [10, 15, 5, 0, 2, 7],
        "E": [4, 5, 6, 7, 8, 9],
    }
)

derived_df = df.drop(["B", "D"], axis=1)

print("The initial DataFrame is:")
print(df, "\n")

print("The DataFrame with A,C and E columns is:")
print(derived_df, "\n")

输出:

The initial DataFrame is:
     A    B     C   D  E
0  302  100   300  10  4
1  504  300   400  15  5
2  708  400   350   5  6
3  103  200   100   0  7
4  343  400  1000   2  8
5  565  700   400   7  9 

The DataFrame with A,C and E columns is:
     A     C  E
0  302   300  4
1  504   400  5
2  708   350  6
3  103   100  7
4  343  1000  8
5  565   400  9 

它从 DataFrame df 中删除 BD 列,并将其余列分配给 derived_df。或者,它选择除 BD 以外的所有列,并将它们分配到 derived_df DataFrame 中。

使用 DataFrame.filter() 方法从 Pandas DataFrame 中选择列

import pandas as pd

df = pd.DataFrame(
    {
        "A": [302, 504, 708, 103, 343, 565],
        "B": [100, 300, 400, 200, 400, 700],
        "C": [300, 400, 350, 100, 1000, 400],
        "D": [10, 15, 5, 0, 2, 7],
        "E": [4, 5, 6, 7, 8, 9],
    }
)

derived_df = df.filter(["A", "C", "E"])

print("The initial DataFrame is:")
print(df, "\n")

print("The DataFrame with A,C and E columns is:")
print(derived_df, "\n")

输出:

The initial DataFrame is:
     A    B     C   D  E
0  302  100   300  10  4
1  504  300   400  15  5
2  708  400   350   5  6
3  103  200   100   0  7
4  343  400  1000   2  8
5  565  700   400   7  9 

The DataFrame with A,C and E columns is:
     A     C  E
0  302   300  4
1  504   400  5
2  708   350  6
3  103   100  7
4  343  1000  8
5  565   400  9

它从 DataFrame df 中提取或过滤 ACE 列,并将其分配给 DataFrame derived_df

作者: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相关文章 - Pandas DataFrame Column