使用熊猫将过滤的行复制到新表格,是否有一种方法可以将旧行#附加到每个复制行的最后一个单元格中?
我正在尝试通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您过滤数据框时,您可以重置索引而不删除旧索引,这将添加一个新列“索引”。我将其重命名为“旧索引”:
如果实际 Excel 行的值相差 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":
If this is out by 1 or so for the actual Excel row (because of a header, etc.), you can then use: