I have a DataFrame df1 and I want to get at a specific date, for example 2022-01-04 all the column names of df1 in a list which would be: 01G, 02G, 04G. So far I was only able to get the number of each row, but not the column names.
This would be a simple example:
df1:
01G 02G 03G 04G
Dates
2022-01-01 0 1 0 1
2022-01-02 1 1 1 0
2022-01-03 0 1 1 1
2022-01-04 1 1 0 1
For reproducibility:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({
'Dates':['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04'],
'01G':[0, 1, 0, 1],
'02G':[1, 1, 1, 1],
'03G':[0, 1, 1, 0],
'04G':[1, 0, 1, 1]})
df1 = df1.set_index('Dates')
np.count_nonzero(df1, axis=1)
Thanks a lot!
CodePudding user response:
Use DataFrame.loc for filter row by datetime, compare for greater like 0 and filter columns names:
print (df1.columns[df1.loc['2022-01-04'].gt(0)].tolist())
['01G', '02G', '04G']
CodePudding user response:
For your special case, it seems we can also filter using the row values directly after changing dtype to bool:
out = df1.columns[df1.loc['2022-01-04'].astype(bool)].tolist()
Output:
['01G', '02G', '04G']
