簡體   English   中英

在 CSV dataframe 中將數十億轉換為數百萬

[英]Converting Billions to Millions in a CSV dataframe

我的問題與 pandas / python 中的格式有關。以下問題已說明。

交易量很大。 將交易量擴大到數百萬股。 例:11714.75萬股擴容后為11714.75萬股。

這就是 dataframe 的樣子。 我需要為所有 125 行修復它。

在此處輸入圖像描述

可能最簡單的方法是將整個列除以一百萬

apple['volume'] = apple['volume'].div(1000000)

您可以通過以下兩種方式替換 117147500 等數字:或者使用浮點數:

import pandas as pd
dictionary = {'Column':[4,5,6,7], 'Volume':[117147500,12000,14000,18000]}
df = pd.DataFrame(dictionary)
df

df_scaled_column=df['Volume']/1000000

# Replace old column with scaled values
df['Volume'] = df_scaled_column
df

Out: 
   Column    Volume
0       4  117.1475
1       5    0.0120
2       6    0.0140
3       7    0.0180

或者用字符串。 特別是我使用了一個 function,我從這個 SE 帖子的答案中找到了它,它在 python 中將長數字格式化為字符串

import pandas as pd
dictionary = {'Column':[4,5,6,7], 'Volume':[117147500,12000,14000,18000]}
df = pd.DataFrame(dictionary)
df

# Function defined in a old StackExchange post
def human_format(num):
    num = float('{:.3g}'.format(num))
    magnitude = 0
    while abs(num) >= 1000:
        magnitude += 1
        num /= 1000.0
    return '{}{}'.format('{:f}'.format(num).rstrip('0').rstrip('.'), ['', 'K', 'M', 'B', 'T'][magnitude])

# Example of what the function does
human_format(117147500) #'117M'

# Create empty list
numbers_as_strings = []

# Fill the empty list with the formatted values
for number in df['Volume']:
    numbers_as_strings.append(human_format(number))

# Create a dataframe with only one column containing formatted values
dictionary = {'Volume': numbers_as_strings}
df_numbers_as_strings = pd.DataFrame(dictionary)

# Replace old column with formatted values
df['Volume'] = df_numbers_as_strings
df

Out: 
   Column Volume
0       4   117M
1       5    12K
2       6    14K
3       7    18K

您可以使用 transform() 方法 ( https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.transform.html ) 並將這些體積數除以 1000,000。

暫無
暫無

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

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