我有这个密码:
for index, row in df.iterrows():
for index1, row1 in df1.iterrows():
if df['budget'].iloc[index] == 0:
if df['production_companies'].iloc[index] == df1['production_companies'].iloc[index1]
and df['release_date'].iloc[index].year == df1['release_year'].iloc[index1] :
df['budget'].iloc[index] = df1['mean'].iloc[index1]
这是可行的,但要花很长时间才能完成。我怎样才能让它跑得更快? 我也试过:
df.where((df['budget'] != 0 and df['production_companies'] != df1['production_companies']
and df['release_date'] != df1['release_year']),
other = pd.replace(to_replace = df['budget'],
value = df1['mean'], inplace = True))
应该快一点,但不行。我如何做到这一点? 谢谢您!你知道吗
df
看起来像这样:
budget; production_companies; release_date ;title
0; Villealfa Filmproduction Oy ;10/21/1988; Ariel
0; Villealfa Filmproduction Oy ;10/16/1986; Shadows in Paradise
4000000; Miramax Films; 12/25/1995; Four Rooms
0; Universal Pictures; 10/15/1993; Judgment Night
42000; inLoops ;1/1/2006; Life in Loops (A Megacities RMX)
...
和df1
:
production_companies; release_year; mean;
Metro-Goldwyn-Mayer (MGM); 1998; 17500000
Metro-Goldwyn-Mayer (MGM); 1999; 12500000
Metro-Goldwyn-Mayer (MGM); 2000; 12000000
Metro-Goldwyn-Mayer (MGM) ;2001 ;43500000
Metro-Goldwyn-Mayer (MGM); 2002 ;12000000
Metro-Goldwyn-Mayer (MGM) ;2003; 36000000
Metro-Goldwyn-Mayer (MGM); 2004 ;27500000
...
如果年份和生产公司相同,我想用来自df
的“mean”vealue替换来自df1
的值0。你知道吗
您可以使用前面的if语句快速删除一个循环:
除去所有的循环,您可以通过合并有效地完成这一点。这里我提供了一些示例数据,因为您提供的所有数据实际上都不会合并。如果
df
中的release_date
不是日期时间,则需要确保它是日期时间。你知道吗然后,如果生产公司和年份匹配,您需要将预算值为0的地方替换为平均值。作为合并,这是:
如果没有给定生产公司和年份的
mean
数据,budget
中的0
将替换为np.NaN
,因此您可以保留它们,也可以根据需要将它们替换回0。你知道吗不要对此任务使用循环
熊猫的主要优点是矢量化的功能。你知道吗
矢量化计算的一种方法是对齐索引,然后使用
pd.DataFrame.index.map
。要提取年份,首先需要转换为datetime
。你知道吗来自@ALollz的数据。你知道吗
相关问题 更多 >
编程相关推荐