Python 在数据帧中的列上迭代(熊猫)
你能解释一下这是怎么回事吗?第5、6和8、9行对我来说没有多大意义,但我只是从Python开始Python 在数据帧中的列上迭代(熊猫),python,pandas,dictionary,Python,Pandas,Dictionary,你能解释一下这是怎么回事吗?第5、6和8、9行对我来说没有多大意义,但我只是从Python开始 1# Iterate over the column in dataframe 2 for entry in col: 3 4 # If entry is in cols_count, add 1 5 if entry in cols_count.keys(): 6 cols_count[entry] += 1 7 # El
1# Iterate over the column in dataframe
2 for entry in col:
3
4 # If entry is in cols_count, add 1
5 if entry in cols_count.keys():
6 cols_count[entry] += 1
7 # Else add the entry to cols_count, set the value to 1
8 else:
9 cols_count[entry] = 1
10
11 # Return the cols_count dictionary
12 return cols_count
cols\u count
是一个计算条目出现次数的字典
向其中添加项的字典语法如下所示:
d = {}
d["new_entry"] = value
第5、6和8、9行所做的是检查字典中是否已经存在该条目,如果存在,则向计数器递增1
;如果没有,则创建该键并指定一个值1
(因为如果条目不在dict中,则这是它的第一次出现)。缩进在Python中是必不可少的。检查一下你的。