3
A
回答
2
我認爲最快的方法是使用convert_objects
和使用標/切割符號,例如選擇最後10列:
In [23]:
df = pd.DataFrame({'a':['1','2','3','4','5']})
df = pd.concat([df]*11, axis=1)
df.columns = list('abcdefghijk')
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 11 columns):
a 5 non-null object
b 5 non-null object
c 5 non-null object
d 5 non-null object
e 5 non-null object
f 5 non-null object
g 5 non-null object
h 5 non-null object
i 5 non-null object
j 5 non-null object
k 5 non-null object
dtypes: object(11)
memory usage: 480.0+ bytes
In [21]:
converted = df[df.columns[-10:]].convert_objects(convert_numeric=True)
converted
Out[21]:
b c d e f g h i j k
0 1 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2 2 2
2 3 3 3 3 3 3 3 3 3 3
3 4 4 4 4 4 4 4 4 4 4
4 5 5 5 5 5 5 5 5 5 5
In [22]:
converted.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 10 columns):
b 5 non-null int64
c 5 non-null int64
d 5 non-null int64
e 5 non-null int64
f 5 non-null int64
g 5 non-null int64
h 5 non-null int64
i 5 non-null int64
j 5 non-null int64
k 5 non-null int64
dtypes: int64(10)
memory usage: 440.0 bytes
然後,您可以直接分配結果回:
In [31]:
df[df.columns[-10:]] = converted
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 11 columns):
a 5 non-null object
b 5 non-null int64
c 5 non-null int64
d 5 non-null int64
e 5 non-null int64
f 5 non-null int64
g 5 non-null int64
h 5 non-null int64
i 5 non-null int64
j 5 non-null int64
k 5 non-null int64
dtypes: int64(10), object(1)
memory usage: 480.0+ bytes
或做在1班輪︰
In [33]:
df[df.columns[-10:]] = df[df.columns[-10:]].convert_objects(convert_numeric=True)
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 11 columns):
a 5 non-null object
b 5 non-null int64
c 5 non-null int64
d 5 non-null int64
e 5 non-null int64
f 5 non-null int64
g 5 non-null int64
h 5 non-null int64
i 5 non-null int64
j 5 non-null int64
k 5 non-null int64
dtypes: int64(10), object(1)
memory usage: 480.0+ bytes
相關問題
- 1. cocoa:如何將整數類型轉換爲字符串類型?
- 2. pandas dataframe將列類型轉換爲字符串或分類
- 3. pandas read_csv將混合類型列轉換爲字符串
- 4. 如何將_sre.SRE_Match類型轉換爲字符串和整數
- 5. 如何將字符串轉換爲整數類型android
- 6. 如何將dtype列轉換爲Pandas Dataframe中的字符串
- 7. 將包含字符串和NAN的列轉換爲Pandas中的整數列表
- 8. 如何將大型字符串轉換爲java中的整數?
- 9. 將包含幾個數字的字符串轉換爲整數
- 10. 如何將字符串列表轉換爲Python中的整數?
- 11. 如何將散列中的字符串轉換爲整數
- 12. 如何將字符串轉換爲整數列表的列表?
- 13. pandas:將字符串列轉換爲有序的類別?
- 14. 如何將數組中的字符串類型轉換爲json
- 15. 不將字符串轉換爲整型
- 16. Fortran77將整型轉換爲字符串
- 17. 如何將字符串中的數字轉換爲整數
- 18. 如何將字符串列表轉換爲整數列表?
- 19. Python Pandas將字符串轉換爲NaN
- 20. Python將Pandas Float轉換爲字符串
- 21. 如何將DataFrame的列名從字符串轉換爲整數?
- 22. PHP類型將字符串轉換爲整數
- 23. 後記:如何將整數轉換爲字符串?
- 24. 未能將pandas數據框中的列轉換爲整數數據類型
- 25. 如何將字符串中的字節轉換爲整數? Python
- 26. 將字符串轉換爲字符串的泛型類型
- 27. 將字符串轉換爲R中的數字數據類型
- 28. 將對象類型列轉換爲數字,字符串等
- 29. 如何將枚舉類型轉換爲字符串列表?
- 30. C#如何將結構類型列表轉換爲字符串?
我認爲'轉換= df [[df.co convert_objects(convert_numeric = True)'應該可以工作 – EdChum
Parens不平衡 – robertevansanders
對不起,試試這個我可以發表一個例子:'df [df.columns [-10:]]。convert_objects(convert_numeric = True )' – EdChum