【问题标题】:saving results in csv file将结果保存在 csv 文件中
【发布时间】:2021-08-15 00:03:29
【问题描述】:

我有两个 csv 文件:aaa 和 bbb。 aaa 文件有 4 列如下 名字

 2  0 0 1
 7  0 1 1
 8  1 1 0

和 bbb 有一列如下 7 8 我想在文件 aaa 中搜索大约 7,8 (bbb),然后在 aaa 中获得整行 7 和 8 作为结果,即我想获得 0 1 1 和 1 1 0 作为结果,然后将其保存在 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])

【问题讨论】:

    标签: python search save


    【解决方案1】:
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-25
      • 2019-06-19
      • 2017-10-04
      • 2019-06-21
      • 2014-02-20
      • 2011-03-21
      • 2016-03-02
      • 2016-08-06
      相关资源
      最近更新 更多