gpt4 book ai didi

python - 如何通过在Python中的某些列上应用条件来过滤csv数据

转载 作者:行者123 更新时间:2023-12-01 03:06:12 24 4
gpt4 key购买 nike

我是Python数据分析新手,在获取特定格式所需的数据时遇到一些问题。

我的数据采用以下格式。 (由于数据较大,请查看附件中的csv格式数据链接)

enter image description here

我使用以下命令以上述格式打印 csv 数据

地址 = 'C:\Barchatdata.csv'
data_c = pd.read_csv(地址)

现在我想在 Energy_Supply_per_capita >280 上应用 if 条件,然后打印索引列、contry_area、Energy_Supply_per_capita 和 Avg_GDP 列。

我尝试了以下命令

data_c.loc[data_c['Energy_Supply_per_capita'] > 280,'Energy_Supply_per_capita']

但仅获得索引和 Energy_Supply_per_capita 列。

如何获得所需的结果?

提前谢谢您。

link to csv file

最佳答案

您可以使用查询

cols = ['Country_Area', 'Energy_Supply_per_capita', 'Avg_GDP']
data_c.query('Energy_Supply_per_capita > 280')[cols]

或者等效地使用 bool 系列和loc

cols = ['Country_Area', 'Energy_Supply_per_capita', 'Avg_GDP']
data_c.loc[data_c.Energy_Supply_per_capita > 280, cols]

关于python - 如何通过在Python中的某些列上应用条件来过滤csv数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43397942/

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