I have created a dataframe using the following code.
JavaScript
x
3
1
df = pd.read_csv('state_cpi.csv')
2
data = df.iloc[:,3:]
3
Then I found the minimum values of each row by using
JavaScript
1
2
1
data.min()
2
It gave me the minimum value of each column, but I also want to find out that at which index the min value is available for each column. Please give a sutiable solution for finding the index of the min value of each column. Dataset in this repo
Advertisement
Answer
First, you have to fix your data. You have --
as value for the cell (347, Himachal Pradesh) then you can use idxmin
as suggested:
JavaScript
1
3
1
df = pd.read_csv('state_cpi.csv', na_values='--')
2
out = df.iloc[:, 3:].idxmin()
3
Output:
JavaScript
1
39
39
1
>>> out
2
Andhra Pradesh 388
3
Arunachal Pradesh 388
4
Assam 388
5
Bihar 388
6
Chattisgarh 388
7
Delhi 388
8
Goa 388
9
Gujarat 388
10
Haryana 388
11
Himachal Pradesh 388
12
Jharkhand 388
13
Karnataka 388
14
Kerala 388
15
Madhya Pradesh 388
16
Maharashtra 388
17
Manipur 388
18
Meghalaya 388
19
Mizoram 358
20
Nagaland 388
21
Orissa 388
22
Punjab 388
23
Rajasthan 388
24
Sikkim 388
25
Tamil Nadu 388
26
Telangana 388
27
Tripura 388
28
Uttar Pradesh 388
29
Uttarakhand 388
30
West Bengal 388
31
Andaman and Nicobar 388
32
Chandigarh 388
33
Dadra and Nagar Haveli 388
34
Daman and Diu 388
35
Jammu and Kashmir 388
36
Lakshadweep 388
37
Puducherry 388
38
dtype: int64
39
Note: it’s really curious but the min for each column is the row 388. Try to use idxmax
to see the difference.