在Python中,可以使用pandas库来处理数据框(DataFrame)操作。要向一个DataFrame中添加满足多个条件的单独DataFrame中的行数的列,可以按照以下步骤进行操作:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14, 15]})
condition_df = df[(df['A'] > 2) & (df['B'] < 9)]
row_count = condition_df.shape[0]
df['New Column'] = row_count
完整的代码如下:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14, 15]})
condition_df = df[(df['A'] > 2) & (df['B'] < 9)]
row_count = condition_df.shape[0]
df['New Column'] = row_count
这样,df中就会添加一个名为"New Column"的列,该列的值为满足条件的行数。
领取专属 10元无门槛券
手把手带您无忧上云