在我的数据框中,我只想根据特定条件从特定列中选择特定列。
Here is my output of initial Data Frame using df.head()
我想找到等于'B'的列,并与选定的列一起显示。
我的代码:
df = pd.read_csv('cancer_data.csv')
#To display column diagnosis equals B
df[df['diagnosis'] == 'B']
#To display selected columns
df[['diagnosis','radius_mean','perimeter_mean','area_mean']]
如何合并条件并仅将其与选定列一起显示。
谢谢
答案 0 :(得分:2)
使用df.loc:
df.loc[df['diagnosis'] == 'B', ['diagnosis','radius_mean','perimeter_mean','area_mean']]