簡體   English   中英

如何從 Pandas to_dict() 輸出中刪除小數

[英]How do I remove decimals from Pandas to_dict() output

這篇文章的要點是,我的原始數據中有“23”,而我希望得到的 dict 中有“23”(而不是“23.0”)。 這是我嘗試用 Pandas 處理它的方法。

我的 Excel 工作表有一個編碼區域列:

23
11
27
(blank)
25

最初,我創建了一個數據框,Pandas 將 Region 的 dtype 設置為float64*

import pandas as pd
filepath = 'data_file.xlsx'
df = pd.read_excel(filepath, sheetname=0, header=0)
df 

23.0
11.0
27.0
NaN
25.0

如果我使用fillna()用似乎消除小數的空白替換 NaN,熊貓會將fillna()轉換為object

df.fillna('', inplace=True)
df

23
11
27
(blank)
25

除了當我將數據幀轉換為字典時我仍然得到小數:

data = df.to_dict('records')
data

[{'region': 23.0,},
 {'region': 27.0,},
 {'region': 11.0,},
 {'region': '',},
 {'region': 25.0,}]

有沒有辦法創建沒有小數位的字典? 順便說一下,我正在編寫一個通用實用程序,所以我不會總是知道列名和/或值類型,這意味着我正在尋找一個通用解決方案(而不是顯式處理 Region)。

非常感謝任何幫助,謝謝!

問題是,在fillna('')之后,盡管列是object類型,但您的基礎值仍然是float

s = pd.Series([23., 11., 27., np.nan, 25.])

s.fillna('').iloc[0]

23.0

相反, apply格式化程序,然后替換

s.apply('{:0.0f}'.format).replace('nan', '').to_dict()

{0: '23', 1: '11', 2: '27', 3: '', 4: '25'}

使用自定義函數,處理整數並將字符串保留為字符串:

import pprint

def func(x):
    try:
        return int(x)
    except ValueError:
        return x

df = pd.DataFrame({'region': [1, 2, 3, float('nan')],
                   'col2': ['a', 'b', 'c', float('nan')]})
df.fillna('', inplace=True)
pprint.pprint(df.applymap(func).to_dict('records'))

輸出:

[{'col2': 'a', 'region': 1},
 {'col2': 'b', 'region': 2},
 {'col2': 'c', 'region': 3},
 {'col2': '', 'region': ''}]

將浮動也保持為浮動的變體:

import pprint

def func(x):
    try:
        if int(x) == x:
            return int(x)
        else:
            return x
    except ValueError:
        return x

df = pd.DataFrame({'region1': [1, 2, 3, float('nan')],
                   'region2': [1.5, 2.7, 3, float('nan')],
                   'region3': ['a', 'b', 'c', float('nan')]})
df.fillna('', inplace=True)
pprint.pprint(df.applymap(func).to_dict('records'))

輸出:

[{'region1': 1, 'region2': 1.5, 'region3': 'a'},
 {'region1': 2, 'region2': 2.7, 'region3': 'b'},
 {'region1': 3, 'region2': 3, 'region3': 'c'},
 {'region1': '', 'region2': '', 'region3': ''}]

你可以添加: dtype=str

import pandas as pd

filepath = 'data_file.xlsx'
df = pd.read_excel(filepath, sheetname=0, header=0, dtype=str)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM