マルチインデックスで読み込んでみるのもよいかと思います。(read_csv()
に header=[0, 1]
を指定)
# Pandas read_csv
import pandas as pd
import io
csv_data = '''
,city1,city1,city1,city2,city2,city2
,Tokyo,Osaka,Nagoya,Tokyo,Osaka,Nagoya
2023/7/1 0:30,4355,44022,51568,45060,48214,6435
2023/7/2 0:30,8372,17772,55602,43017,46247,4357
2023/7/3 0:30,3256,16834,60295,41278,45249,9654
2023/7/4 0:30,3567,42440,67965,39726,44097,5722
2023/7/5 0:30,9843,81794,75601,38656,42693,3567
'''
df1 = pd.read_csv(io.StringIO(csv_data), index_col=0, header=[0, 1])
print(df1)
df1_1 = df1[[('city2', 'Tokyo')]]
print(df1_1)
# city1 city2
# Tokyo Osaka Nagoya Tokyo Osaka Nagoya
# 2023/7/1 0:30 4355 44022 51568 45060 48214 6435
# 2023/7/2 0:30 8372 17772 55602 43017 46247 4357
# 2023/7/3 0:30 3256 16834 60295 41278 45249 9654
# 2023/7/4 0:30 3567 42440 67965 39726 44097 5722
# 2023/7/5 0:30 9843 81794 75601 38656 42693 3567
#
# city2
# Tokyo
# 2023/7/1 0:30 45060
# 2023/7/2 0:30 43017
# 2023/7/3 0:30 41278
# 2023/7/4 0:30 39726
# 2023/7/5 0:30 38656