【发布时间】:2017-08-11 12:20:36
【问题描述】:
我有一个 pandas DataFrame,我想编写一个函数来帮助我将每个负值总结为 result1,并将每个正值总结为 result2。所以基本上,这个函数应该遍历列“total_load”
def total_battery(ok6, col_name='total_load'):
"""Return a dictionary with counts of occurrences."""
# Initialize an empty dictionary: cols_values
cols_values = {}
# Extract column from df: col
col = ok6[col_name]
# iterate over the column in df
for entry in col:
if entry in cols_values.keys() < 0: ***--> then sum all the negative values***
cols_values[entry] += sum
else:
if entry in cols_values.keys() > 0: ***--> then sum all the negative values***
cols_values[entry] += sum
# Return the cols_count dictionary
return cols_values
# Call count_entries(): result1
result1 = total_battery(ok6, "total_load")
# Call count_entries(): result2
result2 = total_battery(ok6, "total_load")
# Print result1 and result2
print(result1)
print(result2)
我希望这不会太令人困惑。
感谢您的帮助。
【问题讨论】:
标签: python function pandas writing