You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python技术问询:将DataFrame数据追加至新表,及按条件提取行的简便方法

Hey there! Let's tackle your pandas DataFrame questions with the simplest, most efficient methods out there—perfect for your 10k-row dataset.

1. Extract Rows Matching name_list to a New DataFrame

The easiest and most performant way to filter rows where the name column is in your name_list is using pandas' isin() method. It creates a boolean mask that you can use to slice your original DataFrame directly:

import pandas as pd

# Assume your original DataFrame is named original_df
# and name_list is your target list of names
filtered_df = original_df[original_df['name'].isin(name_list)]

This method is optimized for large datasets (10k rows is totally trivial for it) and keeps your code clean and readable.

2. Append Another DataFrame to the New One

For appending data, forget the old append() method (it's deprecated in newer pandas versions). The best practice is to use pd.concat()—it's flexible, efficient, and works for single or multiple appends:

Single Append

If you just need to add one DataFrame to filtered_df:

# Assume the DataFrame you want to append is named to_append_df
filtered_df = pd.concat([filtered_df, to_append_df], ignore_index=True)

The ignore_index=True flag resets the index of the combined DataFrame, so you don't end up with duplicate index values.

Multiple Appends (More Efficient)

If you have several DataFrames to append, collect them in a list first and concat once—this is faster than appending one by one:

# List of DataFrames to append
df_list = [filtered_df, df1, df2, df3]
combined_df = pd.concat(df_list, ignore_index=True)

Quick Note:

Make sure all DataFrames you're concatenating have matching column names—if not, pandas will fill missing columns with NaN values.

Hope these straightforward solutions work for you! 😊

内容的提问来源于stack exchange,提问作者Jillian Wittstock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 09:52:51