如何根据条件为pandas数据帧中的新列添加级别



我有一个巨大的数据帧,看起来像这样:

Col1      Col2
0.   'w1'.     'N/A'
1.   'w2'       4.3
2.   'w3'.      1.2
4.   'w4'.      3.5
5.   'w5'      'N/A'
6.   'w6'.      3.1
7.   'w7'.      2.4
8.   'w8'.      1.7
9.   'w9'.      4.6
10.  'w10'.    'N/A'
11.  'w11'.     3.0

我在第一列中有字符串。在第二列中,我有一些行中的浮点值和"N/A"字符串。col1中的字符串是问题的答案选项,col2中的浮点数是答案平均计数和"0";N/A";字符串本身就是问题。我的意图是在这个数据帧中创建另一列;问题编号"每当col2中出现新的"N/a"行时,其电平(Q1、Q2、Q3…(就会发生变化。因此,我想要的输出是:

Col1    Col2      Col3
0.   'Q1'     'w1'.    'N/A'
1.   'Q1'     'w2'      4.3
2.   'Q1'     'w3'.     1.2
4.   'Q1'     'w4'.     3.5
5.   'Q2'     'w5'     'N/A'
6.   'Q2'     'w6'.     3.1
7.   'Q2'.    'w7'.     2.4
8.   'Q2'     'w8'.     1.7
9.   'Q3'     'w9'.    'N/A'
10.  'Q3'     'w10'.    2.0
11.  'Q3'     'w11'.    3.0

有人能帮我吗?我试过这个:

df['question_number']=np.where(df['counts']=='N/A', "Q1", "Q2", 'Q3')

但这不起作用,我也不知道该怎么做。有人能帮忙吗?

更新:@enke的注释比我的代码干净得多。如果要将新列放置在第一个位置,只需先添加列即可。

更新2:在这篇关于添加回答选项列的文章下面,根据您的问题添加更多代码:

import pandas as pd
cols = ['Answer', 'Avg_Score']
data=[['w1', 'N/A'],
['w2', 4.3],
['w3', 1.2],
['w4', 3.5],
['w5', 'N/A'],
['w6', 3.1],
['w7', 2.4],
['w8', 1.7],
['w9', 4.6],
['w10', 'N/A'],
['w11', 3.0]]
df = pd.DataFrame(data, columns = cols)
# insert new answer column in first so it's before the Answer and the Avg_Score
df.insert(0,'answer_option','')
# insert new question number column in the first position
df.insert(0,'question_number','')
# Line from @enke's comment
df['question_number'] = 'Q' + df['Avg_Score'].eq('N/A').cumsum().astype(str)
df['answer_option'] = 'A' + df.groupby('question_number').cumcount().add(1).astype(str)
print(df)

新输出:

question_number answer_option Answer Avg_Score
0               Q1            A1     w1       N/A
1               Q1            A2     w2       4.3
2               Q1            A3     w3       1.2
3               Q1            A4     w4       3.5
4               Q2            A1     w5       N/A
5               Q2            A2     w6       3.1
6               Q2            A3     w7       2.4
7               Q2            A4     w8       1.7
8               Q2            A5     w9       4.6
9               Q3            A1    w10       N/A
10              Q3            A2    w11       3.0

我的原始帖子在下面,如果你想,你可以忽略

我在第一列的位置插入了一个新列,然后循环遍历每一行。我重命名列名只是因为我可以。:D我没有在你的原始df中包括引号和句号。但是下面的代码可能仍然有用。

import pandas as pd
cols = ['Answer', 'Avg_Score']
data=[['w1', 'N/A'],
['w2', 4.3],
['w3', 1.2],
['w4', 3.5],
['w5', 'N/A'],
['w6', 3.1],
['w7', 2.4],
['w8', 1.7],
['w9', 4.6],
['w10', 'N/A'],
['w11', 3.0]]
df = pd.DataFrame(data, columns = cols)
# insert new column before the Answer and the Avg_Score
df.insert(0,'Question','')
# start the question counter at 0
qnum = 0
# loop through each row
for index,row in df.iterrows():
# if 'N/A' found increase the question counter
# this assume first row will always have an 'N/A'
if df.loc[index,'Avg_Score'] == 'N/A':
qnum += 1
df.loc[index,'Question'] = 'Q{}'.format(qnum)
print(df)

输出:

Question Answer Avg_Score
0        Q1     w1       N/A
1        Q1     w2       4.3
2        Q1     w3       1.2
3        Q1     w4       3.5
4        Q2     w5       N/A
5        Q2     w6       3.1
6        Q2     w7       2.4
7        Q2     w8       1.7
8        Q2     w9       4.6
9        Q3    w10       N/A
10       Q3    w11       3.0

最新更新