有两个csv文件:aaa和bbb。Aaa文件有如下4列名称
2 0 0 1
7 0 1 1
8 1 1 0
和BBB有如下一列78我想在文件aaa中搜索大约7,8 (bbb)然后在aaa中得到整行7和8作为结果,即我想得到0 1 1和1 10作为结果然后保存在csv文件中。
aaa=pd.read_csv('aaa.csv',index_col ="name")
bbb=pd.read_csv('bbb.csv', header=None)
c=bbb[0].values
for i in c:
res=aaa.loc[i]
print(res)
for i in range(0,2):
print(res[i])
import csv
def Save(res):
with open('Saved.csv', 'a') as Saved:
cw = csv.writer(Saved)
cw.writerow([res])
df1 = pd.read_csv('aaa.csv')
df2 = pd.read_csv('bbb.csv', header=None)
keepRow = df2[0].values
# Get rows that have on of the keepRow values in column 0
df3 = df1.loc[df1["firstColumn"].isin(keepRow)]
# drop the keepRow column
df3= df3.drop("firstColumn", axis=1)
#write to csv
df3.to_csv("ok.csv", index=False)