Python:如何选择包含一年中特定月份的列?

标签 python pandas dataframe timestamp

我有一个如下所示的数据框:

+------------+-----------+-----+-----+----------+----------+-----+------------+----------+----------+-----+------------+
| State_Name | City_Name | ID1 | ID2 | 1/1/2020 | 1/2/2020 | ... | 12/31/2020 | 1/1/2021 | 1/2/2021 | ... | 12/31/2021 |
+------------+-----------+-----+-----+----------+----------+-----+------------+----------+----------+-----+------------+
| ...        | ...       | ... | ... | ...      | ...      | ... | ...        | ...      | ...      | ... | ...        |
+------------+-----------+-----+-----+----------+----------+-----+------------+----------+----------+-----+------------+
| ...        | ...       | ... | ... | ...      | ...      | ... | ...        | ...      | ...      | ... | ...        |
+------------+-----------+-----+-----+----------+----------+-----+------------+----------+----------+-----+------------+

我有很多从 2020 年 1 月 1 日到 2021 年 12 月 31 日的专栏。如何选择包含一年中特定月份的列?例如,如果我想要选择包含 2021 年 7 月数据的列,我可以对名称为“7/1/2021”、“7/2/2021”……“7/31/2021”的列进行子集化?

非常感谢您的帮助!

最佳答案

您可以使用过滤方法:

df.filter(regex='7/\d{1,2}/2021', axis=1)

关于Python:如何选择包含一年中特定月份的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66710080/

相关文章:

python - 从同一文件导入引发 ImportError

python - 无法在 Python 3 中打印生成器表达式

python - 如何获取特定模型的 id?

python - django外部应用程序安装 "Error: No module named nested-inlines"

python - 如何解决错误 'Cannot do inplace boolean setting on mixed-types with a non np.nan value'

python - 通过对两列进行分组来创建热图

python - 使用 lambda 进行按元素运算 (pd.DataFrame)

python - 将组索引器添加到 pandas 数据框

python - 如何在Python中使用pandas连接同一列中的项目?

python - 为什么 `merge` 之后显示的数据与 pandas 和 jupyter Notebook 中的实际数据帧不同?