티스토리 뷰

SWDesk

DataFrame 예제

bizmaker 2021. 3. 18. 00:56
    def Test01(self):
        Data1 = {
            'D1': ['a1', 'a2', 'a3'],
            'D2': [ 11, 12, 13, 14],
            'D3': [ 31, 32, 33, 34, 35]
        }
        data11 = DataFrame(Data1)
        print(data11)
arrays must all be same length

 

<DataFrame 만들기>

    def Test02(self):
        Data1 = {
            'D1': ['a1', 'a2', np.nan, np.nan,'a3'],
            'D2': [ 11, 12, 13, np.nan, 14],
            'D3': [ 31, 32, 33, 34, 35]
        }
        data11 = DataFrame(Data1)
        print(data11)
    D1    D2  D3
0   a1  11.0  31
1   a2  12.0  32
2  NaN  13.0  33
3  NaN   NaN  34

 

<서버에서 데이터 불러오기>

    def Test11(self):
        url1 = self.TestServer
        param1 = {
            'RequestType': 'LoadDatas',
            'DataNumber': '10',
            'Tablename': 'Articles_901'
        }
        res1 = requests.post(url1, headers=REQUESTHEADER, data=param1)
        res2 = json.loads(res1.text)
        res3 = DataFrame(res2)
        print(res3)
Empty DataFrame
Columns: [SNo, URLAddress, Title, Body, KeywordID, DateInfo, Remarks]
Index: []

 

<DataFrame을 엑셀 파일로 저장하기>

def Test12(self, datas_df, fileName=None):

    wb = Workbook()
    sheet1 = wb.create_sheet('sheet1')
    sheet1.title = "시트1"
    dt1 = datetime.now()
    dateName = dt1.strftime("%Y%m%d%H%M")
    if not fileName:
        fileName = "UnknownTable_"+str(dateName)
    else:
        fileName = fileName+str(dateName)+".xls"


    for row in dataframe_to_rows(datas_df, index=True, header=True):
        if len(row)>1:
            sheet1.append(row)

    wb.save(fileName)
    wb.close()

<DataFrame 내 행 추가>

            data1 = [itemInfo['code'], itemInfo['name'], dateInt, itemInfo['LDate']]
            self.items_Today.loc[excelIndex] = data1

 

 

 

 

반응형