如何修改pandas中groupby操作的输出格式,为大数字生成科学记数法.我知道如何在python中进行字符串格式化,但是在这里应用它时我感到很茫然.
df1.groupby('dept')['data1'].sum()
dept
value1 1.192433e+08
value2 1.293066e+08
value3 1.077142e+08
Run Code Online (Sandbox Code Playgroud)
如果我转换为字符串,这会抑制科学记数法,但现在我只是想知道如何字符串格式和添加小数.
sum_sales_dept.astype(str)
Run Code Online (Sandbox Code Playgroud) python floating-point scientific-notation number-formatting pandas
如何抑制dataframe.describe()的科学记数法输出:
contrib_df["AMNT"].describe()
count 1.979680e+05
mean 5.915134e+02
std 1.379618e+04
min -1.750000e+05
25% 4.000000e+01
50% 1.000000e+02
75% 2.500000e+02
max 3.000000e+06
Name: AMNT, dtype: float64
Run Code Online (Sandbox Code Playgroud)
我的数据类型为float64:
contrib_df["AMNT"].dtypes
dtype('float64')
Run Code Online (Sandbox Code Playgroud) 我有一个数据框,其中有一个名为"Lead Rev"的列.此列是一个数字字段,如(100000或5000等...).我想知道如何格式化这些数字以将逗号显示为千位分隔符.
它是这样的:'{:,}'.format('Lead Rev')
'Lead Rev'是我的专栏名称.我的数据集中有超过200,000行.
()----> 1'{:,}'.format('Lead Rev')中的ValueError Traceback(最近一次调用最后一次)
ValueError:不能用's'指定','或'_'.
说我有数据框,c:
a=np.random.random((6,2))
c=pd.DataFrame(a)
c.columns=['A','B']
Run Code Online (Sandbox Code Playgroud)
打印第0行的值:
print c.loc[(0),:]
Run Code Online (Sandbox Code Playgroud)
结果是:
A 0.220170
B 0.261467
Name: 0, dtype: float64
Run Code Online (Sandbox Code Playgroud)
我想取消显示该Name: 0, dtype: float64行,以便得到:
A 0.220170
B 0.261467
Run Code Online (Sandbox Code Playgroud)
有人知道吗?
(nb我将此附加到文本文件中)
一般来说,关于抑制 Pandas 中的科学记数法存在许多问题
然而,它们似乎都不适用于该to_markdown功能。例如:
import pandas as pd
df = pd.DataFrame({'val': 3e10}, index=[0]) # print(df) gives 3.000000e+10
pd.set_option('float_format', '{:f}'.format) # print(df) gives 30000000000.000000
Run Code Online (Sandbox Code Playgroud)
然而,df.to_markdown()仍然有产出
| | val |
|---:|------:|
| 0 | 3e+10 |
Run Code Online (Sandbox Code Playgroud)
如何禁用 中的科学记数法to_markdown()?
问题设置
import pandas as pd
df = pd.DataFrame(data={'Currency': {0: 111.23, 1: 321.23},
'Int': {0: 23, 1: 3},
'Rate': {0: 0.03030, 1: 0.09840}}
)
Run Code Online (Sandbox Code Playgroud)
生成以下DataFrame
Currency Int Rate
0 111.23 23 0.0303
1 321.23 3 0.0984
Run Code Online (Sandbox Code Playgroud)
我想使用如下所示的dict将非常特定的格式应用于数据框中的每一列:
format_mapping={'Currency': '${:,.2f}', 'Int': '{:,.0f}', 'Rate': '{:.2f}%'}
Run Code Online (Sandbox Code Playgroud)
我知道我可以将applymap用于多列或应用于单个列:
#All columns
df = df.applymap('{:.2f}%'.format)
#Specific columns
df['Rate'] = df['Rate'].apply('{:.2f}%'.format)
Run Code Online (Sandbox Code Playgroud)
题
我怎样才能通过每一列的数据帧进行迭代,并使用字典在应用格式dict key是column和value是string格式化?
最终结果看起来像这样(忽略百分比现在不再乘以100的事实)
Currency Int Rate
0 $111.23 23 0.03%
1 $321.23 3 0.10%
Run Code Online (Sandbox Code Playgroud) 可能这是一个老问题,我在下面找到了类似的问题,但我仍然可以在输出文件中看到科学记数法。
我试图合并set_option和df.apply(pd.to_numeric, args=('coerce',))等下面,而没有工作我的代码。
df = pd.read_csv(Input)
dfNew = df[['co_A','co_B','co_C']]
# I firstly select columns from df then would like to convert scientific notation to decimal type in my output file.
dfNew.to_csv(Output, index = False, sep = '\t')
Run Code Online (Sandbox Code Playgroud)
我仍然可以在输出文件中看到科学记数法。任何人都可以帮忙吗?
co_A co_B co_C
167 0.0 59.6
168 0.0 60.6
191 8e-09 72.6
197 -4.7718e-06 12.3
197 0.0 92.4
198 0.0 39.5
Run Code Online (Sandbox Code Playgroud) pandas ×7
python ×5
dataframe ×3
dictionary ×1
formatting ×1
ipython ×1
numpy ×1
python-2.7 ×1
python-3.x ×1