LogIn E-mail
¼³°èÀ̾߱â
pandas to_excel sheet ¿©·¯Àå excel ÆÄÀÏ ¾²±â Àбâ
# 46 JMJS    24.1.28 15:22

import pandas as pd

import os
os.system('cls')

#----------------------------------------------------------#
# make pandas DataFrame
#----------------------------------------------------------#

sheet1 = pd.DataFrame({'c1': ['A0', 'A1', 'A2', 'A3'],
                       'c2': ['','','',''],
                       'c3': ['C0', 'C1', 'C2', 'C3']},
                       index=['r1', 'r2', 'r3', 'r4'])

row = ['r1','r2','r3']
col = ['c1','c2','c3','c4']
sheet2 = pd.DataFrame([],row,col)

df = pd.Series({'sheet1':sheet1,'sheet2':sheet2})

i,j=10,11
for name,sheet in df.items():
    for r in sheet.index:
        for c in sheet.columns:
            sheet.at[r,c]=j
            j+=1
        i,j=i+10,i+11

#----------------------------------------------------------#
# write excel
#----------------------------------------------------------#
xls_file='pandas_excel.xlsx'
writer = pd.ExcelWriter(xls_file)
df['sheet1'].to_excel(writer,sheet_name='Sheet 1')
df['sheet2'].to_excel(writer,sheet_name='Sheet 2')
writer.close()

#----------------------------------------------------------#
# read excel
#----------------------------------------------------------#
#df= pd.read_excel("cell1.xls") #default header=0,index_col=None,sheet_name=0)
df = pd.read_excel(xls_file,index_col=0,sheet_name=['Sheet 1','Sheet 2'])

sheet1 = df.get('Sheet 1')
sheet2 = df.get('Sheet 2')

for sheet in df.keys():
    print("#-----------------------------------------#")
    print("# sheet:",sheet)
    print("#-----------------------------------------#")
    print("  index:",df[sheet].index.tolist())
    print("columns:",df[sheet].columns.ravel())
    print("columns[2: ]=",df[sheet].columns[1:])
    print("columns[ :2]=",list(df[sheet].columns[:2]))
    print("at['r1','c2']=",df[sheet].at['r1','c2'])
    #----------------------------------------------------------#
    # write csv
    #----------------------------------------------------------#
    print(df[sheet].to_csv(index=True))

°Ô½Ã¹°: 61 °Ç, ÇöÀç: 1 / 1 ÂÊ
¹øÈ£ Á¦       ¸ñ ÀÛ¼ºÀÚ µî·ÏÀÏ ¹æ¹®
61  os.path.dirname(os.path.realpath(__file__))) JMJS 25.8.20 36
60  img2rgb.py PIL lambda print end prn sys.argv JMJS 24.6.18 260
59  cv2 image pixel bgr °ª ¼öÁ¤Çϱâ JMJS 24.3.1 285
58  ±ÛÀÚÇǶó¹Ìµå lpyramid.py JMJS 24.3.1 329
57  µñ¼Å³Ê¸® dictionary µñ¼Å³Ê¸® key¿Í value µÚÁý±â ... JMJS 24.3.1 295
56  bus idx2set cap JMJS 24.2.22 276
55  ÆÄÀ̽㠸®½ºÆ®¿¡¼­ ºó ¹®ÀÚ¿­ÀÎ ¿ø¼Ò Á¦°ÅÇϱâ JMJS 24.2.22 272
54  openpyxl JMJS 24.2.22 298
53  Decorators µ¥ÄÚ·¹ÀÌÅÍ JMJS 24.2.29 285
52  walrus operator := and for in list JMJS 24.2.1 287
51  pandas Çà Ãß°¡ JMJS 24.1.28 281
50  split with brackets JMJS 24.1.27 297
49  ÀÌÅÍ·¹ÀÌÅÍ Iterator JMJS 24.1.26 296
48  re regular expression JMJS 24.1.26 293
47  read_excel()ÀÇ ÁöÁ¤ °¡´ÉÇÑ ¿É¼Ç JMJS 24.1.26 283
46  pandas to_excel sheet ¿©·¯Àå excel ÆÄÀÏ ¾²±â Àбâ JMJS 24.1.28 310
45  pandas ¼¿ isna isnull notna notnull JMJS 24.1.28 310
44  tkinter tk1.py JMJS 24.1.25 321
43  pandas _append df2.py JMJS 24.1.25 299
42  ÆÇ´Ù½º pandas DataFrame, list, dictionary, ... JMJS 24.1.25 299
41  f-string JMJS 24.6.18 328
40  pandas JMJS 24.1.17 284
39  __add__ __getattr__ __getitem__ __setitem__ JMJS 24.1.24 304
38  ÆÄÀ̽㠼³Ä¡°æ·Î È®ÀÎ JMJS 23.12.4 299
37  deepcopy JMJS 23.11.24 312
36  def func(*args): JMJS 23.11.24 301
35  sys.version_info JMJS 23.11.19 336
34  argparse add_argument parse_args JMJS 24.1.17 304
33  [bpy]bone2_wr.py JMJS 23.9.23 316
32  [bpy]bm.verts depsgraph JMJS 23.9.23 303
31  [bpy]keyframe_insert JMJS 23.9.23 314
30  [bpy]key_blocks JMJS 23.9.23 312
29  def abc (a=10,b=20,c=30) JMJS 23.8.1 317
28  shape prj JMJS 23.10.29 329
27  yaml JMJS 23.11.30 343
26  getopt arg argv JMJS 23.7.26 331
25  pg.mouse.get_pressed() pg.mouse.get_pos() JMJS 22.11.20 417
24  pg.MOUSEBUTTONDOWN event.button JMJS 22.11.20 400
23  ¸ðµÎÀÇ ÆÄÀ̽ã - 20Àϸ¸¿¡¹è¿ì´Â ÇÁ·Î±×·¡¹Ö ±âÃÊ JMJS 20.1.8 449
22  python -m pip install -U pip JMJS 19.11.25 454
21  get =lambda i,m ... JMJS 19.11.13 456
20  2Â÷¿ø ¸®½ºÆ®, Æ©Çà JMJS 19.11.13 446
19  [turtle]def polygon3(n,c,d) JMJS 19.11.11 463
18  [turtle]def polygon2(n,a) JMJS 19.11.11 458
17  def factorial(n) JMJS 19.11.11 446
16  def sum_func(n) JMJS 19.11.11 449
15  def hello() fun1(a) fun2(a,b) JMJS 19.11.11 448
14  while sum JMJS 19.11.11 446
13  random calculation JMJS 19.11.11 441
12  turtle random JMJS 19.11.11 449
11  import time time.time JMJS 19.11.11 454
10  turtle begin_fill end_fill JMJS 19.11.11 443
9  if else JMJS 19.11.11 435
8  input JMJS 19.11.11 443
7  list JMJS 24.3.1 461
6  turtle triangle JMJS 19.11.11 439
5  for range JMJS 19.11.11 447
4  ¸ðµâ »ç¿ëÇϱâ JMJS 19.11.11 446
3  import ÆÄÀ̽㠸ðµâ °¡Á®´Ù ¾²±â JMJS 19.11.11 454
1  print ÁÙ¹Ù²Þ JMJS 19.11.11 503
 ^_^ 70.1.1
[1]