gpt4 book ai didi

python - 如果行中的项目满足列之间的条件,则追加到新的 df

转载 作者:行者123 更新时间:2023-12-02 02:07:35 27 4
gpt4 key购买 nike

我正在尝试通过“正确”和“不正确”的答案来组织一个大数据集。正确答案的条件是:

df['color'] == 'green' and df['correct'] = 'v') or (df['color']=='blue' and df['correct']='a') or (df['color']=='red' and df['correct']=='r')

如果行满足这些条件,我想将它们附加到 df_ Correct:

df_correct = pd.DataFrame(columns=['word','rt'])

如果不满足条件,我需要将行附加到另一个数据帧。

我想过循环遍历数据框的行,但这似乎不起作用:

df_correct = pd.DataFrame(columns=['word','rt'])
df_incorrect = pd.DataFrame(columns=['word','rt'])

for i in df:
if (df['color'] == 'green' and df['correct'] = 'v') or (df['color']=='blue' and df['correct']='a') or (df['color']=='red' and df['correct']=='r'):
df_correct.append(i)
else:
df_incorrect.append(i)

这就是 df 的一部分:

,word,color,correct,rt

923,ABUSO,red,r,993
929,CHALECO, blue, a,1825
935,ESTATUA, red, r,951
941,ORQUESTRA, blue, a,1393
1556,METAL, blue, a,1285
1562,ABUSO,red,r,1272
1568,CALLE, green, v,1210
1574,CORTINA, blue, a,872
1580,SILLA, blue, a,530
1586,COBARDE, blue, a,510
1592,TRISTEZA, green, v,786
1598,PÁRPADO, green, v,561
1604,ACCIDENTE, blue, a,1329
1853,VENTANA, red, r,1010
1859,ABUSO,red,r,752
1919,CANASTA, blue, a,628
1925,TERRORISTA, green, v,589
1931,BRAZO, red, r,970
1937,TENEDOR, red, r,784
1943,SILENCIO, red, r,614
1949,TRISTEZA, green, v,564
1955,POSTER, green, v,1314
1961,MORGUE, green, v,703
1967,LLUVIA, blue, a,898
1973,VIOLÍN, green, v,638
1979,POBREZA, red, r,724
1985,TRAICIÓN, green, v,856
1991,UTENSILLO, green, v,942
1997,CÁNCER, red, r,845
2003,BANCO, green, v,927
2009,DESASTRE, green, v,769
2015,SILLA, blue, a,999
2021,SOLEDAD, red, r,647
2027,ESFERA, red, r,637
2033,MISERIA, blue, a,823
2039,ESTATUA, red, r,793
2045,DEDO, red, r,566
2051,VIOLACIÓN, blue, a,840
2057,HEBILLA, blue, a,818
2063,ENOJO, red, r,731
2069,DEMONIO, green, v,858
2075,CABALLO, green, v,767
2081,CORTINA, blue, a,763
2087,ASIENTO, green, v,800
2093,CRUEL, red, r,651
2099,MIEDO, green, v,895
2105,EDIFICIO, blue, a,726
2111,PANFLETO, red, r,713
2117,DEPRESIÓN, red, r,598
2123,MANTEL, green, v,578
2129,PARTE, blue, a,580
2135,CÁRCEL, green, v,791
2141,ORQUESTRA, blue, a,569
2147,INFIERNO, red, r,519
2153,MOMENTO, red, r,506
2159,DIABLO, blue, a,680
2165,CUCARACHA, green, v,627
2171,BOMBA, red, r,483
2177,GANGRENA, red, r,514
2183,MUERTE, blue, a,895
2189,AHOGADO, red, r,630
2195,PARALISIS, blue, a,743
2201,VENENO, green, v,646
2207,CHALECO, blue, a,662
2213,ODIO, green, v,792
2219,FUNERAL, green, v,641
2225,MONJA, green, v,728
2231,LÁPIZ, red, r,676
2237,METAL, blue, a,590
2243,PAPEL, green, v,614
2249,CUADRADO, blue, a,795
2255,ACCIDENTE, blue, a,665
2261,CASA, red, r,662
2267,DOLOR, blue, a,781
2273,CHOQUE, green, v,596
2366,HEBILLA, blue, a,1573
2372,BANCO, green, v,732
2378,VIOLÍN, green, v,608
2384,ACCIDENTE, blue, a,564
2390,ASIENTO, green, v,687
2396,SANGRIENTO, blue, a,820
2402,GANGRENA, red, r,951
2408,ENOJO, red, r,708
2414,AHOGADO, red, r,786
2420,TENEDOR, red, r,643
2426,ESTATUA, red, r,427
2432,DOLOR, blue, a,779
2438,MENTÓN, red, r,670
2444,CHALECO, blue, a,450
2450,NUBES, green, v,418
2456,ÍTEM, red, r,627
2462,DEPRESIÓN, red, r,835
2468,CORDERO, blue, a,770
2474,INFIERNO, red, r,781
2480,DIABLO, blue, a,542
2486,SILENCIO, red, r,486
2492,ORQUESTRA, blue, a,734
2498,POSTER, green, v,902
2504,PARALISIS, blue, a,1055
2510,QUEMADURA, green, v,657
2516,LLUVIA, blue, a,557
2522,COBARDE, blue, a,485
2528,CASA, red, r,550
2534,TERRORISTA, green, v,483
2540,ASALTO, blue, a,725
2546,POBREZA, red, r,580
2552,SILLA, blue, a,601
2558,CORTINA, blue, a,467
2564,FUNERAL, green, v,493
2570,MANTECA, green, v,466
2576,ABUSO,red,r,779
2582,PARTE, blue, a,557
2756,CANASTA, blue, a,477
2762,BRAZO, red, r,567
2768,ODIO, green, v,608
2774,MIEDO, green, v,503
2780,LÁPIZ, red, r,561
2786,EDIFICIO, blue, a,529
2792,MATANZA, red, r,740
2798,BOMBA, red, r,404
2804,METAL, blue, a,601
2810,PÁRPADO, green, v,390
2816,CABALLO, green, v,391
2822,MOMENTO, red, r,1270
2828,PIE, green, v,654
2834,MANTEL, green, v,510
2840,GRANJA, blue, a,477
2846,TRAICIÓN, green, v,544
2852,CALLE, green, v,500
2858,CHOQUE, green, v,510
2864,PARAGUAS, blue, a,574
2957,MISERIA, blue, a,1938
2963,SOLEDAD, red, r,1426
2969,LLUVIA, blue, a,1091
2975,TENEDOR, red, r,1181
2981,CORTINA, blue, a,1012
2987,PESADILLA, red, r,865
2993,PARALISIS, blue, a,793
2999,ACCIDENTE, blue, a,776
3005,DEDO, red, r,1669
3011,MENTÓN, red, r,665
3017,TORTURA, blue, a,622
3023,UTENSILLO, green, v,1353
3029,CUADRADO, blue, a,1693
3143,MANTECA, green, v,1028
3149,PÁRPADO, green, v,1257
3155,CADÁVER, red, r,1192
3161,PANFLETO, red, r,560
3167,EDIFICIO, blue, a,1181
3173,CORDERO, blue, a,485
3179,PIE, green, v,1145
3233,BANCO, green, v,989
3239,BOMBA, red, r,935
3371,ABUSO,red,r,981

由于它们都符合条件,因此它们应该转到 df_ Correct。

正确的做法是什么?谢谢。

最佳答案

如果我理解正确,请尝试创建条件,然后根据条件过滤 df:

c1 = df['color'].eq('green') & df['correct'].eq('v')
c2 = df['color'].eq('blue') & df['correct'].eq('a')
c3 = df['color'].eq('red') & df['correct'].eq('r')
m = c1 | c2 | c3

然后使用loc基于完整条件m选择值:

correct_df = df.loc[m]
incorrect_df = df.loc[~m]

可选还选择特定列:

correct_df = df.loc[m, ['word', 'rt']]
incorrect_df = df.loc[~m, ['word', 'rt']]

示例数据框:

df = pd.DataFrame({'color': ['green', 'blue', 'red'] * 2,
'correct': ['v', 'r', 'v', 'a', 'a', 'r'],
'word': list('abcdef'),
'rt': range(1, 7)})
   color correct word  rt
0 green v a 1
1 blue r b 2
2 red v c 3
3 green a d 4
4 blue a e 5
5 red r f 6

正确_df:

correct_df = df.loc[m]

color correct word rt
0 green v a 1
4 blue a e 5
5 red r f 6

correct_df = df.loc[m, ['word', 'rt']]

word rt
0 a 1
4 e 5
5 f 6

in Correct_df:

incorrect_df = df.loc[~m]

color correct word rt
1 blue r b 2
2 red v c 3
3 green a d 4

incorrect_df = df.loc[~m, ['word', 'rt']]

word rt
1 b 2
2 c 3
3 d 4

关于python - 如果行中的项目满足列之间的条件,则追加到新的 df,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68198298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com