简体   繁体   English

使用 Python 从 dataframe 用逗号替换点

[英]Replacing dot with comma from a dataframe using Python

I have a dataframe for example df:我有一个 dataframe 例如df:

I'm trying to replace the dot with a comma to be able to do calculations in excel.我正在尝试用逗号替换点,以便能够在 excel 中进行计算。

I used:我用了:

df = df.stack().str.replace('.', ',').unstack()

or或者

df = df.apply(lambda x: x.str.replace('.', ','))

Results:结果:

Nothing changes but I receive his warning at the end of an execution without errors:没有任何变化,但我在执行结束时收到他的警告,没有错误:

FutureWarning: The default value of regex will change from True to False in a future version. FutureWarning:正则表达式的默认值将在未来版本中从 True 更改为 False。 In addition, single character regular expressions will not be treated as literal strings when regex=True.此外,当 regex=True 时,单字符正则表达式不会被视为文字字符串。


View of what I have:查看我所拥有的:

数据框到excel

Expected Results:预期成绩:

在此处输入图像描述

Updated Question for more information thanks to @Pythonista anonymous:感谢@Pythonista 匿名更新问题以获取更多信息:

print(df.dtypes)

returns:返回:

Date         object
Open         object
High         object
Low          object
Close        object
Adj Close    object
Volume       object
dtype: object

I'm extracting data with the to_excel method:我正在使用 to_excel 方法提取数据:

df.to_excel()

I'm not exporting the dataframe in a.csv file but an.xlsx file我不是在 a.csv 文件中导出 dataframe 而是在 an.xlsx 文件中导出

Where does the dataframe come from - how was it generated? dataframe 从何而来——它是如何产生的? Was it imported from a CSV file?它是从 CSV 文件导入的吗?

Your code works if you apply it to columns which are strings, as long as you remember to do df = df.apply() and not just df.apply() , eg:只要您记得执行df = df.apply()而不仅仅是df.apply() ,您的代码就可以将其应用于字符串列,例如:

import pandas as pd
df = pd.DataFrame()
df['a'] =['some . text', 'some . other . text']
df = df.apply(lambda x: x.str.replace('.', ','))
print(df)

However, you are trying to do this with numbers, not strings.但是,您尝试使用数字而不是字符串来执行此操作。 To be precise, the other question is: what are the dtypes of your dataframe?准确地说,另一个问题是:您的 dataframe 的 dtype 是什么? If you type如果你输入

df.dtypes

what's the output? output 是什么?

I presume your columns are numeric and not strings, right?我想你的列是数字而不是字符串,对吧? After all, if they are numbers they should be stored as such in your dataframe.毕竟,如果它们是数字,它们应该这样存储在您的 dataframe 中。

The next question: how are you exporting this table to Excel?下一个问题:你是如何将这个表导出到 Excel 的?

If you are saving a csv file, pandas' to_csv() method has a decimal argument which lets you specify what should be the separator for the decimals (tyipically, dot in the English-speaking world and comma in many countries in continental Europe).如果您要保存 csv 文件,pandas 的to_csv()方法有一个decimal参数,可让您指定小数点的分隔符(通常在英语世界中为点,在欧洲大陆的许多国家中为逗号)。 Look up the syntax .查找语法

If you are using the to_excel() method, it shouldn't matter because Excel should treat it internally as a number, and how it displays it (whether with a dot or comma for decimal separator) will typically depend on the options set in your computer.如果您使用的是 to_excel() 方法,则无关紧要,因为 Excel 应在内部将其视为数字,以及如何显示它(无论是用点还是逗号作为小数分隔符)通常取决于您在计算机。

Please clarify how you are exporting the data and what happens when you open it in Excel: does Excel treat it as a string?请说明您如何导出数据以及在 Excel 中打开它时会发生什么: Excel 是否将其视为字符串? Or as a number, but you would like to see a different separator for the decimals?或者作为数字,但您希望看到不同的小数分隔符?

Also look here for how to change decimal separators in Excel: https://www.officetooltips.com/excel_2016/tips/change_the_decimal_point_to_a_comma_or_vice_versa.html Also look here for how to change decimal separators in Excel: https://www.officetooltips.com/excel_2016/tips/change_the_decimal_point_to_a_comma_or_vice_versa.html

UPDATE更新

OP, you have still not explained where the dataframe comes from. OP,您还没有解释 dataframe 的来源。 Do you import it from an external source?您是否从外部来源导入它? Do you create it/ calculate it yourself?您自己创建/计算它吗? The fact that the columns are objects makes me think they are either stored as strings, or maybe some rows are numeric and some are not.列是对象的事实使我认为它们要么存储为字符串,要么有些行是数字的,有些则不是。

What happens if you try to convert a column to float?如果您尝试将列转换为浮点数会发生什么?

df['Open'] = df['Open'].astype('float64')

If the entire column should be numeric but it's not, then start by cleansing your data.如果整个列应该是数字但不是,那么首先清理您的数据。

Second question : what happens when you use Excel to open the file you have just created?第二个问题:当你使用 Excel 打开你刚刚创建的文件时会发生什么? Excel displays a comma, but what character Excel sues to separate decimals depends on the Windows/Mac/Excel settings, not on how pandas created the file. Excel 显示一个逗号,但 Excel 要求分隔小数的字符取决于 Windows/Mac/Excel 设置,而不取决于 pandas 创建文件的方式。 Have you tried the link I gave above, can you change how Excel displays decimals?您是否尝试过我上面给出的链接,您可以更改 Excel 显示小数的方式吗? Also, does Excel treat those numbers as numbers or as strings?此外,Excel 是否将这些数字视为数字或字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM