【问题标题】:I am trying to predict the behavior of covid-19 in Colombia, in the Country_Region column, delete all the rows that have countries other than Colombia我正在尝试预测 covid-19 在哥伦比亚的行为,在 Country_Region 列中,删除所有包含哥伦比亚以外国家的行
【发布时间】:2020-10-25 19:16:12
【问题描述】:

我一直在 Python 中使用 Pandas 的 Crop 命令来删除行和列。我正在尝试预测 covid-19 在哥伦比亚的行为。现在,我需要在 Country_Region 列中删除所有包含哥伦比亚以外国家的行,您能帮帮我吗?

数据(.csv):https://drive.google.com/file/d/1eEZfBmMQTlJjx1PSmC3bamukqhxR_oAy/view?usp=sharing

Python1 Python2

【问题讨论】:

  • 在一天结束时,您想拥有哥伦比亚的所有行 - 对吧?顺便说一句:您能否将您的数据集提供给公众查看?
  • 谢谢!数据集可用
  • columbia_df = df[df['Country_Region'] == 'Columbia'] 有效吗?这将过滤到 Country_Region 列等于“Columbia”的行
  • 是的,它的工作,谢谢。但现在不会删除我不需要的列:(
  • 您可以将结果写入新的 csv(或数据库、变量等)。

标签: python-3.x pandas csv


【解决方案1】:

此问题的目的是分离 Country_Region == Colombia 的数据。 这使您有机会继续进行进一步的评估。

  1. 步骤:读取 csv 数据
  2. 步骤:应用选择
  3. 步骤:只取结果
# Do the import
import pandas as pd

# Step 1: Row 0 is defined as the header of the object
df = pd.read_csv('10-01-2020.csv', header=0)

# Apply the selection
df2 = df[df['Country_Region'] == 'Colombia']

# Write the result into a new csv at the same place the script is existing
df2.to_csv('answer.csv')

结果 answer.csv 中只有哥伦比亚的行。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多