Does someone know how to display titles and indexes in the way described below?
in this sample example, we can see a display of title and boolean result( code: df.isnull().any() )
But what I need is titles and indexes display in this way.
e.g:
State False
District False
Persons False
Males False
Females False
Growth..1991...2001. False
Rural False
Urban True
Number.of.households False
Household.size..per.household. True
dtype: bool
Can someone help? Thanks forward.
CodePudding user response:
You could use df.columns
to get the column labels of the dataframe and df.index
to get the index.
CodePudding user response:
You can solve this program by using loc
property.
Let's create a dataset
import pandas as pd
import numpy as np
data = np.arange(1,13)
data = data.reshape(3,4)
columns = ['Apple','Car','Sport','Food']
index = ['Alice','Bob','Emma']
df = pd.DataFrame(data=data,index=index,columns=columns)
df
Got the output as
Apple Car Sport Food
Alice 1 2 3 4
Bob 5 6 7 8
Emma 9 10 11 12
Then we can use loc property. The loc property is used to access a group of rows and columns by label(s) or a boolean array.
.loc[]
is primarily label based, but may also be used with a boolean array. Mention the row you want.
df.loc['Bob',:]
Got the output
Output
Apple 5
Car 6
Sport 7
Food 8
Name: Bob, dtype: int64
You can access specific columns too.
df.loc['Bob',['Car','Food']]
Car 6
Food 8
Name: Bob, dtype: int64
For the index name
df.index
Output
Index(['Alice', 'Bob', 'Emma'], dtype='object')
For the columns
df.columns
Output
Index(['Apple', 'Car', 'Sport', 'Food'], dtype='object')