首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CSV数据中转换数十亿到数百万

在CSV数据中转换数十亿到数百万
EN

Stack Overflow用户
提问于 2022-04-28 12:47:58
回答 3查看 230关注 0票数 -1

我的问题与熊猫/巨蟒的格式有关。现提出以下问题。

交易量很大。将成交量扩大到数百万股。例:十七万七千四百七千七千五百股规模后,将成为一百一十七十四万七千五百万股。

这就是数据文件的样子。我需要把它全部修复125行。

EN

回答 3

Stack Overflow用户

发布于 2022-04-28 12:59:26

最简单的方法可能是把整列除以一百万。

代码语言:javascript
复制
apple['volume'] = apple['volume'].div(1000000)
票数 1
EN

Stack Overflow用户

发布于 2022-04-28 13:01:14

您可以通过以下两种方式替换像117147500这样的数字:或者用浮点数:

代码语言:javascript
复制
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

或者用绳子。特别是,我使用了一个函数,它是从SE post formatting long numbers as strings in python的答案中找到的。

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2022-04-28 13:06:04

您可以使用transform()方法(https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.transform.html)并将这些卷号除以1000 000。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72043986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档