简体   繁体   English

列出的熊猫数据框系列-抑制浮动科学计数法

[英]Pandas Dataframe Series To List - Suppress Float Scientific Notation

I have a Pandas DataFrame with a float column that I convert to a list, then convert to a string, and then write to a text file for another use. 我有一个带有浮点列的Pandas DataFrame,我将其转换为列表,然后转换为字符串,然后写入文本文件以供其他使用。

For example: 例如:

df=pd.DataFrame([[0.0068149439999999999, 0.90550613999999996], [7.5699999999999997e-05, 0.48159182100000003], 
          [0.009679478, 0.50158892700000002], [0.020792602, 0.56266469899999993]], columns=['ColumnA', 'ColumnB'])

TextToWrite='ColumnA = (' + str(df['ColumnA'].tolist())[1:-1] + ')'
f=open('myfile.txt', 'w')
f.write(TextToWrite)

However, I need the converted floats to not be in Scientific notation (7.5699999999999997e-05, in this example). 但是,我需要转换后的浮点数不要采用科学计数法(在此示例中为7.5699999999999997e-05)。 What is the best way suppress scientific notation for these floats and does it make more sense to do it upfront in the Pandas DataFrame or after the series is already converted to a list? 抑制这些浮点数的科学方法的最好方法是什么?在Pandas DataFrame中或在系列已转换为列表之后提前做些有意义吗?

I looked into the "float_format" argument, which can be done with "to_csv" and "to_string" commands, but I'm not sure how I can do that and still write the string to the text file in the same format I do above. 我查看了“ float_format”参数,可以使用“ to_csv”和“ to_string”命令完成此操作,但是我不确定如何做到这一点,并且仍然将字符串以与上面相同的格式写入文本文件。

Ordinarily you can use astype(str) , which is better for rounding. 通常,您可以使用astype(str) ,这对于四舍五入更好。 But since your numbers are so small, you'll have to explicitly suppress scientific notation via '%f' . 但是由于您的数字太小,您必须通过'%f'明确地禁止科学计数法。

def to_str(x): return '%f' % x
', '.join(df.ColumnA.apply(to_str).values)

(just realized I'm late with an answer but I'll leave this an an alternate answer if you want finer control over the output format) Just replace the TextToWrite line with: (只是意识到我迟到了一个答案,但是如果您想更好地控制输出格式,我会给它一个替代答案),只需将TextToWrite行替换为:

TextToWrite = 'ColumnA = ' + ' %12.7f'*4 % tuple( df['ColumnA'].tolist() )

to get this: 得到这个:

ColumnA = 0.0068149 0.0000757 0.0096795 0.0207926

Word of caution, you need to choose your format to provide enough precision since this will force decimal formatting over exponential even if the result is 0.0. 提醒您,您需要选择格式以提供足够的精度,因为即使结果为0.0,这也会强制十进制格式超过指数格式。 For example, if you use %12.1 you will just get a bunch of zeroes. 例如,如果您使用%12.1,则只会得到一堆零。

Btw, I believe (from testing) that chrisaycock's %f is equivalent to %8.6f which should be fine here but if you have other values in your data that are smaller (say, .00000009) then you might not get enough precision with %f. 顺便说一句,我(从测试中)相信chrisaycock的%f等效于%8.6f,在这里应该没问题,但是如果数据中的其他值较小(例如.00000009),则%可能无法获得足够的精度F。

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

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