如何用Pandas删除公共索引值?

How to delete common index values with Pandas?

我有一个来自 csv 文件的 pandas df。所有条目的索引列中都有一个公共值。我怎样才能删除这个共同价值?常用值为 '00:00:00'

 Date/Time
 2021-01-04 00:00:00                           Compost Maker
 2021-01-05 00:00:00                    Green Up Feed & Weed
 2021-01-05 00:00:00              Nippon Mouse Trap in a Box
 2021-01-06 00:00:00                  Organic Liquid Seaweed
 2021-01-06 00:00:00                  Organic Rooting Powder                                      
 Name: Product, dtype: object

尝试:

df.index = pd.to_datetime(df.index).normalize()

结果:

print(df)

                               Product
Date/Time                             
2021-01-04               Compost Maker
2021-01-05        Green Up Feed & Weed
2021-01-05  Nippon Mouse Trap in a Box
2021-01-06      Organic Liquid Seaweed
2021-01-06      Organic Rooting Powder

这会将您的索引设置为字符串,删除“00:00:00”部分:

df.index = [str(df.index[i]).replace(" 00:00:00", "") for i in range(len(df))]
df['Date/Time'] = df['Date/Time'].str.extract(r'(\d+-\d+-\d+)')

使用正则表达式应该可以。