免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6173 | 回复: 3

初学小白求助,每次循环产生的LIST怎么保存下来? [复制链接]

论坛徽章:
0
发表于 2019-05-21 21:20 |显示全部楼层
导出只有只有list(n3)里面的内容,求助!
  1. import pandas as pd
  2. import os
  3. pwd=os.getcwd()
  4. workpath='C:\\Users\\CIB\\Desktop'
  5. os.chdir(workpath)


  6. condi_vn = [(40000,80000),(20000,40000),(7500,15000),(2500,5000)]
  7. condi_worth = [(20000000,40000000),(10000000,20000000),(4000000,8000000),(1500000,3000000)]
  8. condi_scale = ['大型','中型','小型','微型']


  9. data=pd.read_excel('2019年4月核心客户档案库(3000户月报模版)结果数.XLSX',header=0,
  10.                  sheet_name='201904',encoding='gbk')

  11. final=pd.DataFrame()
  12. for scale,vn,worth in zip(condi_scale,condi_vn,condi_worth):
  13.         
  14.         
  15.     n1 = data[(vn[0] < data['4月月均VN']) & (data['4月月均VN']<= vn[1])
  16.                               & (worth[0] < data['4月财富业务年日均']) & (data['4月财富业务年日均']<= worth[1])]['单位名称']
  17.     n2 = data[(vn[0] < data['4月月均VN']) & (data['4月月均VN'] <= vn[1])
  18.                                & (data['4月财富业务年日均'] <= worth[0])]['单位名称']
  19.     n3 = data[(data['4月月均VN'] <= vn[0])
  20.                               & (worth[0] < data['4月财富业务年日均']) & (data['4月财富业务年日均'] <= worth[1])]['单位名称']
  21.    
  22.     temp = list(n1)+list(n2)+list(n3)
  23.    
  24.     cus = pd.DataFrame(temp)
  25.     final=cus
  26.    
  27. final.to_excel('output.xls')
复制代码


论坛徽章:
0
发表于 2019-05-22 16:42 |显示全部楼层
>>> import pandas as pd
>>> d = {'col1': [1, 2], 'col2': [3, 4]}
>>> df = pd.DataFrame(data=d)
>>> df
   col1  col2
0     1     3
1     2     4
>>>
>>>
>>> d = {'col11': [1, 2], 'col12': [3, 4]}
>>> df = pd.DataFrame(data=d)
>>> df
   col11  col12
0      1      3
1      2      4
-----------------------------------------
final.to_excel('output.xls') 应该写在循环里面

论坛徽章:
0
发表于 2019-05-22 16:43 |显示全部楼层
final.to_excel('output.xls') 这个也要追加模式,不知道这里支持不

论坛徽章:
0
发表于 2019-05-22 17:13 |显示全部楼层
>>> d = {'col1': [1, 2], 'col2': [3, 4]}
>>> df = pd.DataFrame(data=d, columns=['col1', 'col2'])
>>>
>>>
>>> df
   col1  col2
0     1     3
1     2     4
>>>
>>>
>>>
>>> dd = {'col1': [11111, 2222], 'col2': [33333, 444444]}
>>> ddf = pd.DataFrame(data=dd, columns=['col1', 'col2'])
>>> df.append(ddf)
    col1    col2
0      1       3
1      2       4
0  11111   33333
1   2222  444444

也可以这种方式。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP