我试图assign在pandas df. 具体来说,对于df下面的内容,我想用它Column['On']来确定当前发生了多少个值。然后我想将这些值以3. 所以值;
1-3 = 1
4-6 = 2
7-9 = 3 etc
这可以达到 20-30 个值。我考虑过 np.where 但它不是很有效而且我返回了一个错误。
import pandas as pd
import numpy as np
d = ({
'On' : [1,2,3,4,5,6,7,7,6,5,4,3,2,1],
})
df = pd.DataFrame(data=d)
此调用有效:
df['P'] = np.where(df['On'] == 1, df['On'],1)
但是,如果我想将此应用于其他值,则会出现错误:
df = df['P'] = np.where(df['On'] == 1, df['On'],1)
df = df['P'] = np.where(df['On'] == 2, df['On'],1)
df = df['P'] = np.where(df['On'] == 3, df['On'],1)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
饮歌长啸
沧海一幻觉
相关分类