我在 Pandas Dataframe 的“B”列中有值,可以是正值也可以是负值:
data=[[5889.25, 738.0],
[5896.5, 49.0],
[5897.5, 130.0],
[5899.5, -266.0],
[5903.75, -126.0],
[5903.75, -512.0],
[5898.75, -141.0],
[5897.5, -303.0],
[5895.0, -107.0],
[5893.25, 27.0]]
pd.DataFrame(data,columns=['A','B'])
A B
0 5889.25 738.0
1 5896.50 49.0
2 5897.50 130.0
3 5899.50 -266.0
4 5903.75 -126.0
5 5903.75 -512.0
6 5898.75 -141.0
7 5897.50 -303.0
8 5895.00 -107.0
9 5893.25 27.0
什么是快速 pythonic 方法来使计算“B”中的数字为负数的“C”列?所以数据框看起来像:
A B C
0 5889.25 738.0 0
1 5896.50 49.0 0
2 5897.50 130.0 0
3 5899.50 -266.0 1
4 5903.75 -126.0 2
5 5903.75 -512.0 3
6 5898.75 -141.0 4
7 5897.50 -303.0 5
8 5895.00 -107.0 6
9 5893.25 27.0 0
请您参考如下方法:
您可以使用 np.where 找到负值,然后使用 groupby 和 cumcount()+1
data=[[5889.25, 738.0],
[5896.5, 49.0],
[5897.5, 130.0],
[5899.5, -266.0],
[5903.75, -126.0],
[5903.75, -512.0],
[5898.75, -141.0],
[5897.5, -303.0],
[5895.0, -107.0],
[5893.25, 27.0]]
df = pd.DataFrame(data,columns=['A','B'])
df['C'] = np.where(df['B']>0,0,df.groupby(np.where(df['B']<0,0,df['B'])).cumcount()+1)
输出:
A B C 0 5889.25 738.0 0 1 5896.50 49.0 0 2 5897.50 130.0 0 3 5899.50 -266.0 1 4 5903.75 -126.0 2 5 5903.75 -512.0 3 6 5898.75 -141.0 4 7 5897.50 -303.0 5 8 5895.00 -107.0 6 9 5893.25 27.0 0
If you want to create a sequence for every positive number you can write a function
count = 0
def count_neg(x):
global count
if x < 0:
count+=1
else :
count = 0
return count
df['C'] = df['B'].apply(count_neg)
输出:
A B C 0 5889.25 -738.0 1 1 5896.50 -49.0 2 2 5897.50 130.0 0 3 5899.50 -266.0 1 4 5903.75 -126.0 2 5 5903.75 -512.0 3 6 5898.75 -141.0 4 7 5897.50 -303.0 5 8 5895.00 -107.0 6 9 5893.25 27.0 0
