import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
df_new = pd.DataFrame(np.random.randn(5,3), columns=list('ABC'))
display(df_new)
diff_BC= []
for i in range(len(df_new)):
diff_BC.append(df_new.loc[i]['B'] - df_new.loc[i]['C'])
df_new['difference'] = diff_BC
display(df_new['difference'])
print(type(i))
我不明白df_new.loc[i]['B'] - df_new.loc[i]['C']
为何起作用,我认为应该给出一个错误,因为type(i)
是int
,应该使用iloc
。