convert decimal to string python
<p>I imported tab delimited file to create a dataframe (<code>df</code>) which has the following <code>label</code>:</p>

<pre><code>label
1
2

3

1
</code></pre>

<p>This is stored as pandas.core.series.Series and I want to convert it to string format so that I can get rid of the decimals when I write this out to a text file.</p>

<pre><code>df.class_label=df.label.fillna('')
df.to_string(columns=['label'],index=False)
</code></pre>

<p>The variable type is still <code>Series</code>, and output (text file) also has the decimals:</p>

<pre><code>1.0 2.0  3.0  1.0
</code></pre>

<p>How to get rid of these decimals?</p>

<p>Using <code>astype(int)</code> will change a <code>float</code> to an <code>int</code> and will drop your <code>.0</code> as desired.</p>

<pre><code>import pandas as pd
df = pd.DataFrame({'label': [1.0, 2.0, 4.0, 1.0]})
print(df)

    label
0   1.0
1   2.0
2   4.0
3   1.0

df.label = df.label.astype(int)
print(df)

    label
0   1
1   2
2   4
3   1
</code></pre>

<p>Here we do not need to convert this to a string. This will be done when exporting to <code>.csv</code> or <code>.txt</code> and will preserve the <code>int</code>.</p>

python pandas decimal tostring