0
我想操縱一個熊貓df,這樣我就可以計算出一列中某個數據點出現的頻率,在另一列發生特定事件之後。下面的僞代碼可能總結得最好。任何幫助將非常感激!熊貓列操縱
import datetime
import time
import pandas as pd
# Set number of rows to skip
rows_to_skip = 0
# Rows to use after skipped rows
rows_to_use = 10000
# Read the file (Adjust arguments accordingly)
data = pd.read_csv('example.csv',skiprows=rows_to_skip, error_bad_lines=False, nrows=rows_to_use, low_memory=False)
# Add headers when skipping rows
data.columns = ["X","Y","Z"]
# Psuedo Code Below
for variable in data['X']:
if variable > 0:
# Count number of times the following conditions are met in all subsequent rows:
condition 1) Y > 0
condition 2) Z <= Z of the row where variable was > 0
# Then I want to add the total count to a new column, and have it in the same row as X when the "variable" > 0.
任何幫助?
斯特凡,謝謝。很有幫助。但是我試圖計算在X> 0的實例之後但在X> 0的下一行之前兩個條件同時滿足的次數(data.Y> 0和data.Z <= row.Z)。再次感謝! –
這就是我認爲的第一個例子。 – Stefan
我已經更新,只留下解決您的問題的第一個例子。請讓我知道,如果你有任何問題。 – Stefan