Home > Net >  How to avoid inserting zeros in dataframe pandas
How to avoid inserting zeros in dataframe pandas

Time:08-13

For example i have value in excel cell (37)%. With command

df2[cell]=df2[cell].astype(str).str.replace(r'\D', ' ')

I get value 0 37, but I want only to have 37. Any help?

The output of df2[cell].to_list() is

[3.04, -0.37, nan, -1.0, -0.44, -0.48, -0.25, -0.27, -0.59, -0.2, -0.09, 0.03, -0.42, -0.69, -0.04, -0.76, -0.66, -0.21, -0.53, -0.47, -0.1, -0.12, -0.47, 0.19, -0.21, 0.2, nan, -0.33, -0.06, 0.04, 0.44]

CodePudding user response:

Assuming you want strings as output:

df2['out'] = df2['cell'].mul(100).astype(str).str.extract('(\d )', expand=False)

output:

   cell  out
0  3.04  304
1 -0.37   37
2   NaN  NaN
3 -1.00  100
4 -0.44   44
5 -0.48   48
6 -0.25   25

If you want integers:

df2['out'] = df2['cell'].mul(100).astype('Int64').abs()

output:

   cell   out
0  3.04   304
1 -0.37    37
2   NaN  <NA>
3 -1.00   100
4 -0.44    44
5 -0.48    48
6 -0.25    25
  • Related