熊猫较新,并尝试从文件 ABC 中存在的文件 XYZ 中删除每一行。
代码:
auto
输入删除条件后,它将正确打印以下行:
import pandas as pd
# Reads two excel files
clm1 = pd.read_csv('ABC.csv')
clm2 = pd.read_csv('XYZ.csv')
# Prints file length
print('Main file clm2: '+ str(len(clm2['image_url'])))
print('Referral file clm1': str(len(clm1['Input.image_url'])))
for index1 in clm1.index:
for index2 in clm2.index:
if clm2['image_url'][index2] == clm1['Input.image_url'][index1]:
print("Entered into deletion condition!!")
print(clm2['image_url'][index2])
print(clm1['Input.image_url'][index1])
print('\n \n')
clm2.drop(clm2['image_url'][index2], axis=0, inplace=True)
print('Deleted!!')
print('Main file clm2: ' + str(len(clm2['image_url'])))
但是在网上遇到错误:
print(clm2['image_url'][index2])
print(clm1['Input.image_url'][index1])
print('\n \n')
错误说:
clm2.drop(clm2['image_url'][index2], axis=0, inplace=True)
如何解决这个问题?
答案 0 :(得分:1)
如果您的csv看起来像这样,这应该可以工作:
XYZ.csv:
name,value
a,1
b,2
c,3
d,4
e,5
f,6
ABC.csv:
name,value
a,1
b,2
c,3
d,4
代码:
import pandas as pd
import numpy as np
xyz = pd.read_csv("XYZ.csv", index_col='name')
abc = pd.read_csv("ABC.csv", index_col='name')
for i in abc.index:
if i in xyz.index:
xyz.drop(i, axis=0, inplace=True)
print(xyz)