df.loc和df.iloc之间的区别

时间:2019-11-10 11:37:41

标签: python python-3.x pandas

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

0 个答案:

没有答案