我想将每个第二个值的第一列的行值更改为“绿色”,将其余的值更改为“红色”(请参见下文,我是使用replace方法开始的,但是不太有效。
color IntDen Density Condition
0 green 936645 Low Ctrl
1 red 1571204 Low Ctrl
2 green 1154038 Low Ctrl
3 red 1423050 Low Ctrl
4 5 691283 Low Aß42
5 6 764449 Low Aß42
6 7 638492 Low Aß42
7 8 887382 Low Aß42
8 9 788693 Low H20
9 10 1401201 Low H20
10 11 936645 Low H20
11 12 1571204 Low H20
12 13 1122389 Low LPS
13 14 2262593 Low LPS
14 15 866009 Low LPS
15 16 1398048 Low LPS
16 17 960636 High Ctrl
17 18 1939803 High Ctrl
18 19 1994001 High Ctrl
19 20 3399789 High Ctrl
20 21 810265 High Aß42
21 22 1113727 High Aß42
22 23 1095145 High Aß42
23 24 1402325 High Aß42
24 25 1424273 High H20
25 26 2771698 High H20
26 27 1807418 High H20
27 28 1265651 High H20
28 29 2046300 High LPS
29 30 1225662 High LPS
30 31 1755320 High LPS
31 32 1425662 High LPS
答案 0 :(得分:1)
如果默认值为2
,则对每个索引值使用numpy.where
和模数RangeIndex
:
df['color'] = np.where(df.index % 2, 'red', 'green')
一般解决方案类似,仅按长度df
创建辅助数组:
df['color'] = np.where(np.arange(len(df)) % 2, 'red', 'green')
print (df)
color IntDen Density Condition
0 green 936645 Low Ctrl
1 red 1571204 Low Ctrl
2 green 1154038 Low Ctrl
3 red 1423050 Low Ctrl
4 green 691283 Low As42
5 red 764449 Low As42
6 green 638492 Low As42
7 red 887382 Low As42
8 green 788693 Low H20
9 red 1401201 Low H20
10 green 936645 Low H20
11 red 1571204 Low H20
12 green 1122389 Low LPS
13 red 2262593 Low LPS
14 green 866009 Low LPS
15 red 1398048 Low LPS
另一个想法是通过索引设置值:
pos = df.columns.get_loc('color')
df.iloc[::2, pos] = 'green'
df.iloc[1::2, pos] = 'red'
类似的numpy解决方案:
#pandas 0.24+
arr = df['color'].to_numpy()
#pandas below
#arr = df['color'].values
arr[::2] = 'green'
arr[1::2] = 'red'
df['color'] = arr