如何在Python中用逗号或点分隔DataFrame中的数字?

时间:2019-08-22 11:48:17

标签: python python-3.x

我需要对Python中的100000到100.000之类的数字进行一些转换。我该怎么办?

我尝试使用Format(number,'。d')。但是我有DataFrame或List。当我执行代码时,它说不支持的格式字符串传递给numpy.ndarray。格式

users = data.iloc[:,[3]].values.astype(str)


for i in range(len(users)):
    users[i] = (format (users[i], ',d')) 
users[i] = (format (users[i], ',d'))
  

TypeError:不支持的格式字符串传递给numpy.ndarray。格式

2 个答案:

答案 0 :(得分:1)

这是不使用format函数而是依靠语言环境模块来实现您要执行的操作的另一种方法:

<div id="gallery" style="display: flex; flex-wrap: wrap; justify-content: center;">
  <figure style="background-color:#2A3132; width: 350px; height: 280px">
    <img src="https://via.placeholder.com/110x110">
    <a href="#">
      <img src="https://via.placeholder.com/30">
    </a>
  </figure>
</div>

结果:

import pandas
import locale
locale.setlocale(locale.LC_ALL, 'de_DE.utf-8')

users = pandas.DataFrame(data=[100, 1000, 10000, 100000], columns=["colA"])
users["colA"] = [locale.format("%d", v, 1) for v in users.iloc[:,0]]
print(users)

答案 1 :(得分:0)

您基本上是在系列上应用格式,也没有指定轴。

尝试此操作,它将逗号添加到第三列的所有值:

df.iloc[:,[3]] = df.iloc[:,[3]].apply(lambda x:format (x.values[0], ',d'), axis=1)