すべてのファイルから文字列として読み込まれた文字列を、あなたは整数で構成されて列を持っていると仮定すると浮く、と、あなたはこのようなものがあるでしょう:
s = pd.Series(['10', '20', '30.4', '40.7', 'text', 'more text', '50.0'])
、その場合には、あなた文字列に整数(背面)に変換するための第2の機能次に、整数を浮動小数点数に変換する関数を適用することができる:ここ
import pandas as pd
def print_type(x):
print(type(x))
return x
def to_int(x):
try:
# x is a float or an integer, and will be returned as an integer
return int(pd.to_numeric(x))
except ValueError:
# x is a string
return x
def to_str(x):
return str(x)
s = pd.Series(['10', '20', '30.4', '40.7', 'text', 'more text', '50.0'])
s2 = s.apply(to_int).apply(to_str)
print("Series s:")
print(s)
print("\nSeries s2:")
print(s2)
print("\nData types of series s2:")
print(s2.apply(print_type))
は最終的に、各数が変換された、ことを示し、出力されます整数の文字列バージョン:
Series s:
0 10
1 20
2 30.4
3 40.7
4 text
5 more text
6 50.0
dtype: object
Series s2:
0 10
1 20
2 30
3 40
4 text
5 more text
6 50
dtype: object
Data types of series s2:
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
0 10
1 20
2 30
3 40
4 text
5 more text
6 50
dtype: object
これがあなたの後ろにあるかどうかはわかりませんが、もしそうでない場合は、どうやって始めたらよいかが分かります。これは、あなたが望むように文字列をフォーマットするよりもstrにキャストするために、パンダ0.19.2:
を使用しています。 – MedAli
コードを投稿できますか?これらのデータ型をすべて1つの列に持つデータフレームはありますか? ID列はどのような型ですか?私はあなたの問題を再現したいが、何をすべきかわからない。 –
サンプルデータを提供し、受け取っているトレースバックを提供する。私は、文字列をintにキャストすることは非論理的であるということを理解していません。これはどういう意味ですか?数字を表す文字列ではない文字列があることを意味しますか? –