使用熊猫将过滤的行复制到新表格,是否有一种方法可以将旧行#附加到每个复制行的最后一个单元格中?

发布于 2025-01-20 13:49:30 字数 579 浏览 2 评论 0原文

我正在尝试通过python过滤大表格,我将.xlsx读为具有特定值的滤波行,然后将每个匹配行输出到新的数据框中。

但是,我的问题是我想附加一个新的列,该列仅包含来自原始表格中的“旧”行号。我不太确定如何有效地完成这项工作。

有人可以将我指向正确的方向吗?

import pandas as pd

df = pd.read_excel(path, sheet_name='Sheet 1')

matching_term_list = ['TRUE', 'FALSE']
column_name = 'True/False/NA'

df_filtered_command = df[df[col_name_type].isin(matching_term_list)]
df_filtered_command.columns = column_headers
df_filtered_command.head()

with pd.ExcelWriter('new_sheet.xlsx') as writer:
    df_filtered_command.to_excel(writer, sheet_name="Command", index=False)

I'm trying to filter a large sheet through Python, I read the .xlsx into a new dataframe, filter rows that have a specific value, then output each matching row into a new dataframe.

However, my issue is I would like to append a new column that simply contains the "old" row numbers from the origin sheet. I'm not really sure how to accomplish this efficiently.

Can someone point me in the right direction?

import pandas as pd

df = pd.read_excel(path, sheet_name='Sheet 1')

matching_term_list = ['TRUE', 'FALSE']
column_name = 'True/False/NA'

df_filtered_command = df[df[col_name_type].isin(matching_term_list)]
df_filtered_command.columns = column_headers
df_filtered_command.head()

with pd.ExcelWriter('new_sheet.xlsx') as writer:
    df_filtered_command.to_excel(writer, sheet_name="Command", index=False)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好多鱼好多余 2025-01-27 13:49:31

当您过滤数据框时,您可以重置索引而不删除旧索引,这将添加一个新列“索引”。我将其重命名为“旧索引”:

df_filtered_command = df[df[col_name_type].isin(matching_term_list)].reset_index(drop=False).rename(columns={"index": "Old Index"})

如果实际 Excel 行的值相差 1 左右(由于标题等原因),则可以使用:

df_filtered_command["Old Index"] += 1

When you filter the dataframe, you can then reset the index and not drop the old one, which will add a new column "index". I the renamed it to be "Old Index":

df_filtered_command = df[df[col_name_type].isin(matching_term_list)].reset_index(drop=False).rename(columns={"index": "Old Index"})

If this is out by 1 or so for the actual Excel row (because of a header, etc.), you can then use:

df_filtered_command["Old Index"] += 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文