【问题标题】:Returning all rows in a dataframe after searching a column with a list of numbers - Python/Pandas在使用数字列表搜索列后返回数据框中的所有行 - Python/Pandas
【发布时间】:2020-05-13 03:31:41
【问题描述】:

我一直在尝试在数据框中搜索数字列表,每次在列中匹配一个数字时,我想返回整行并将其保存到新的数据框,然后保存到 Excel。

millreflist 是数字列表 - 可以是随机长度。

TUCABCP 是我正在搜索的数据框。

PO 是我要在其中搜索数字的列。

我已经使用 .loc 尝试了下面的代码,但是在打开新的 excel 文件时,我只得到了标题,没有行或数据。

millreflistlength = len(millreflist)

for i in range(millreflistlength): TUCABCP = TUCABCP.loc[TUCABCP['PO'] == millreflist[i]]

TUCABCP.to_excel("NEWBCP.xlsx", header=True, index=False)

我使用了以下问题作为参考,但它不包括您何时想使用数字列表进行搜索:Selecting rows from a Dataframe based on values in multiple columns in pandas

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

试试这样的:

## Get list, where each element is the index of a row which you want to keep
indexes = TUCABCP[TUCABCP['PO'].isin(millreflist)]
## Filter the original df to get just the rows with indexes in the list
df = TUCABCP[TUCABCP.index.isin(indexes)]

【讨论】:

  • 只需要df = TUCABCP.loc[TUCABCP['PO'].isin(millreflist)]
  • @ansev 您的回答有效。只是好奇为什么我的代码什么也没返回?
  • @Shah 你在每个循环中重写了TUCABCP 的值。我猜'PO' 列中没有最后一个元素为millreflist 的行,所以你输出了一个空的df
  • 如果您使用与重写文件相同的 Excel 名称。另外,这里不需要循环。您可以使用Series.isin 循环很慢
猜你喜欢
  • 2021-09-06
  • 2017-08-31
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
相关资源
最近更新 更多