123

次のcsvをint64ではなく文字列としてインポートしたいと思います。Pandas read_csv は自動的に int64 に変換しますが、この列は文字列として必要です。

ID
00013007854817840016671868
00013007854817840016749251
00013007854817840016754630
00013007854817840016781876
00013007854817840017028824
00013007854817840017963235
00013007854817840018860166


df = read_csv('sample.csv')

df.ID
>>

0   -9223372036854775808
1   -9223372036854775808
2   -9223372036854775808
3   -9223372036854775808
4   -9223372036854775808
5   -9223372036854775808
6   -9223372036854775808
Name: ID

残念ながら、コンバーターを使用しても同じ結果が得られます。

df = read_csv('sample.csv', converters={'ID': str})
df.ID
>>

0   -9223372036854775808
1   -9223372036854775808
2   -9223372036854775808
3   -9223372036854775808
4   -9223372036854775808
5   -9223372036854775808
6   -9223372036854775808
Name: ID
4

3 に答える 3

193

繰り返しますが、これは pandas >= 0.9.1 で動作します:

In [2]: read_csv('sample.csv', dtype={'ID': object})
Out[2]: 
                           ID
0  00013007854817840016671868
1  00013007854817840016749251
2  00013007854817840016754630
3  00013007854817840016781876
4  00013007854817840017028824
5  00013007854817840017963235
6  00013007854817840018860166

整数オーバーフローの検出に関する問題も作成しています。

編集: ここで解決策を参照してください: https://github.com/pydata/pandas/issues/2247

他の人に役立つように更新します。

すべての列を str にするには、これを行うことができます (コメントから):

pd.read_csv('sample.csv', dtype = str)

ほとんどの列または選択的な列を str として持つには、次のようにします。

# lst of column names which needs to be string
lst_str_cols = ['prefix', 'serial']
# use dictionary comprehension to make dict of dtypes
dict_dtypes = {x : 'str'  for x in lst_str_cols}
# use dict on dtypes
pd.read_csv('sample.csv', dtype=dict_dtypes)
于 2012-11-14T17:58:57.017 に答える
21

これはおそらく最もエレガントな方法ではありませんが、仕事は完了します。

In[1]: import numpy as np

In[2]: import pandas as pd

In[3]: df = pd.DataFrame(np.genfromtxt('/Users/spencerlyon2/Desktop/test.csv', dtype=str)[1:], columns=['ID'])

In[4]: df
Out[4]: 
                       ID
0  00013007854817840016671868
1  00013007854817840016749251
2  00013007854817840016754630
3  00013007854817840016781876
4  00013007854817840017028824
5  00013007854817840017963235
6  00013007854817840018860166

'/Users/spencerlyon2/Desktop/test.csv'ファイルへのパスに置き換えるだけです

于 2012-11-09T02:54:20.790 に答える