猿问

基于python中的逻辑表达式使用for循环创建新列

我有以下数据框:


df1 = pd.DataFrame()

df1 ['TG'] = [0,2,1,3,5,7,]

df1['Value'] =[0.2,0.5,0.015,0.6,0.11,0.12]

我想根据 TG 列的值(即 <1、<2、<3、<4 和 >0、>1、>2、>3 等)创建新列。列名将是 U0.5, U1.5,U2.5,U3.5,O0.5,O1.5,O2.5,O3.5 因此,我将有 8 个具有上述列名的新列。每个单元格的值将来自值列。我的预期输出如下:

我可以使用 np.where 一次创建一个新列。

谁能告诉我如何循环执行?


呼啦一阵风
浏览 460回答 2
2回答

繁花不似锦

使用 numpy 广播,因此不需要循环:#create arrayarr = np.arange(1, 5) - .5print (arr)[0.5 1.5 2.5 3.5]#create Mx1 arrays from Seriesvals = df1['Value'].values[:, None]tg = df1['TG'].values[:, None]#compare arrays and multiple, use DataFrame constructordf2 = pd.DataFrame((arr > tg) * vals, columns=arr).add_prefix('U')df3 = pd.DataFrame((arr < tg) * vals, columns=arr).add_prefix('O')#join all togetherdf = pd.concat([df1, df2, df3], axis=1)print (df)&nbsp;&nbsp;&nbsp; &nbsp;TG&nbsp; Value&nbsp; U0.5&nbsp; &nbsp;U1.5&nbsp; &nbsp;U2.5&nbsp; &nbsp;U3.5&nbsp; &nbsp;O0.5&nbsp; O1.5&nbsp; O2.5&nbsp; O3.50&nbsp; &nbsp;0&nbsp; 0.200&nbsp; &nbsp;0.2&nbsp; 0.200&nbsp; 0.200&nbsp; 0.200&nbsp; 0.000&nbsp; 0.00&nbsp; 0.00&nbsp; 0.001&nbsp; &nbsp;2&nbsp; 0.500&nbsp; &nbsp;0.0&nbsp; 0.000&nbsp; 0.500&nbsp; 0.500&nbsp; 0.500&nbsp; 0.50&nbsp; 0.00&nbsp; 0.002&nbsp; &nbsp;1&nbsp; 0.015&nbsp; &nbsp;0.0&nbsp; 0.015&nbsp; 0.015&nbsp; 0.015&nbsp; 0.015&nbsp; 0.00&nbsp; 0.00&nbsp; 0.003&nbsp; &nbsp;3&nbsp; 0.600&nbsp; &nbsp;0.0&nbsp; 0.000&nbsp; 0.000&nbsp; 0.600&nbsp; 0.600&nbsp; 0.60&nbsp; 0.60&nbsp; 0.004&nbsp; &nbsp;5&nbsp; 0.110&nbsp; &nbsp;0.0&nbsp; 0.000&nbsp; 0.000&nbsp; 0.000&nbsp; 0.110&nbsp; 0.11&nbsp; 0.11&nbsp; 0.115&nbsp; &nbsp;7&nbsp; 0.120&nbsp; &nbsp;0.0&nbsp; 0.000&nbsp; 0.000&nbsp; 0.000&nbsp; 0.120&nbsp; 0.12&nbsp; 0.12&nbsp; 0.12循环解决方案:arr = np.arange(1, 5) - .5for x in arr:&nbsp; &nbsp; df1[f"U{x}"] = df1["Value"] * (df1["TG"] < x)for x in arr:&nbsp; &nbsp; df1[f"O{x}"] = df1["Value"] * (df1["TG"] > x)print (df1)&nbsp; &nbsp;TG&nbsp; Value&nbsp; U0.5&nbsp; &nbsp;U1.5&nbsp; &nbsp;U2.5&nbsp; &nbsp;U3.5&nbsp; &nbsp;O0.5&nbsp; O1.5&nbsp; O2.5&nbsp; O3.50&nbsp; &nbsp;0&nbsp; 0.200&nbsp; &nbsp;0.2&nbsp; 0.200&nbsp; 0.200&nbsp; 0.200&nbsp; 0.000&nbsp; 0.00&nbsp; 0.00&nbsp; 0.001&nbsp; &nbsp;2&nbsp; 0.500&nbsp; &nbsp;0.0&nbsp; 0.000&nbsp; 0.500&nbsp; 0.500&nbsp; 0.500&nbsp; 0.50&nbsp; 0.00&nbsp; 0.002&nbsp; &nbsp;1&nbsp; 0.015&nbsp; &nbsp;0.0&nbsp; 0.015&nbsp; 0.015&nbsp; 0.015&nbsp; 0.015&nbsp; 0.00&nbsp; 0.00&nbsp; 0.003&nbsp; &nbsp;3&nbsp; 0.600&nbsp; &nbsp;0.0&nbsp; 0.000&nbsp; 0.000&nbsp; 0.600&nbsp; 0.600&nbsp; 0.60&nbsp; 0.60&nbsp; 0.004&nbsp; &nbsp;5&nbsp; 0.110&nbsp; &nbsp;0.0&nbsp; 0.000&nbsp; 0.000&nbsp; 0.000&nbsp; 0.110&nbsp; 0.11&nbsp; 0.11&nbsp; 0.115&nbsp; &nbsp;7&nbsp; 0.120&nbsp; &nbsp;0.0&nbsp; 0.000&nbsp; 0.000&nbsp; 0.000&nbsp; 0.120&nbsp; 0.12&nbsp; 0.12&nbsp; 0.12

SMILET

如果你仍然想要一个循环,有一种简单而优雅的方法来做到这一点:l = [0.5, 1.5, 2.5, 3.5]for item in l:&nbsp; &nbsp; df1["U" + str(item)] = df1["Value"] * (df1["TG"] < item)&nbsp; &nbsp; df1["O" + str(item)] = df1["Value"] * (df1["TG"] > item)输出是:TG&nbsp; Value&nbsp; &nbsp;U0.5&nbsp; &nbsp; O0.5&nbsp; &nbsp; U1.5&nbsp; &nbsp; O1.5&nbsp; &nbsp; U2.5&nbsp; &nbsp; O2.5&nbsp; &nbsp; U3.5&nbsp; &nbsp; O3.50&nbsp; &nbsp;0&nbsp; &nbsp;0.200&nbsp; &nbsp;0.2 0.000&nbsp; &nbsp;0.200&nbsp; &nbsp;0.00&nbsp; &nbsp; 0.200&nbsp; &nbsp;0.00&nbsp; &nbsp; 0.200&nbsp; &nbsp;0.001&nbsp; &nbsp;2&nbsp; &nbsp;0.500&nbsp; &nbsp;0.0 0.500&nbsp; &nbsp;0.000&nbsp; &nbsp;0.50&nbsp; &nbsp; 0.500&nbsp; &nbsp;0.00&nbsp; &nbsp; 0.500&nbsp; &nbsp;0.002&nbsp; &nbsp;1&nbsp; &nbsp;0.015&nbsp; &nbsp;0.0 0.015&nbsp; &nbsp;0.015&nbsp; &nbsp;0.00&nbsp; &nbsp; 0.015&nbsp; &nbsp;0.00&nbsp; &nbsp; 0.015&nbsp; &nbsp;0.003&nbsp; &nbsp;3&nbsp; &nbsp;0.600&nbsp; &nbsp;0.0 0.600&nbsp; &nbsp;0.000&nbsp; &nbsp;0.60&nbsp; &nbsp; 0.000&nbsp; &nbsp;0.60&nbsp; &nbsp; 0.600&nbsp; &nbsp;0.004&nbsp; &nbsp;5&nbsp; &nbsp;0.110&nbsp; &nbsp;0.0 0.110&nbsp; &nbsp;0.000&nbsp; &nbsp;0.11&nbsp; &nbsp; 0.000&nbsp; &nbsp;0.11&nbsp; &nbsp; 0.000&nbsp; &nbsp;0.115&nbsp; &nbsp;7&nbsp; &nbsp;0.120&nbsp; &nbsp;0.0 0.120&nbsp; &nbsp;0.000&nbsp; &nbsp;0.12&nbsp; &nbsp; 0.000&nbsp; &nbsp;0.12&nbsp; &nbsp; 0.000&nbsp; &nbsp;0.12然后您需要重新排列列顺序
随时随地看视频慕课网APP

相关分类

Python
我要回答