I have an pandas DataFrame that looks like this:
output:
0 1 3 ...
0 Subject Importance Hour ...
1 Linear Algebra Mandatory Afternoon ...
2 Data Science Optional Morning ...
3 Data Structure Mandatory Night ...
4 ... ... ...
What I know so far is that, I can get the value of the Subject
with:
df[0].tolist()
That will generate an list like:
['Linear Algebra', 'Data Science', 'Data Structure', ...]
But I need to create an dictionary that has the name of the subject, and the value defined as False
subject_dict = {
'Linear Algebra': False,
'Data Science' : False,
'Data Structure': False
}
So that I can access them in order to modify from false to true. My first though was to use a dictionary, but thinking right know I may use also with a list.
How can I solve this?
I've tried:
subject_dict = df[0].to_dict()
but it didn't work.
Perhaps in a list but how it's going to be the iteration?
CodePudding user response:
You can use dict-comprehension:
dct = {s: False for s in df[0][1:]]
Or use dict.fromkeys
:
dct = dict.fromkeys(df[0][1:], False)
Output:
>>> dct
{'Linear Algebra': False,
'Data Science': False,
'Data Structure': False}