在Pandas系列中将字符串转换为浮点数

时间:2017-10-15 15:00:52

标签: python string pandas csv series

我想用(13MB)数据集绘制“MJD”与“MJD_DUPLICATE”的关系 DR14Q_pruned_repeats.csv“在这里找到:: https://www.dropbox.com/s/1dyong27bre3p9j/DR14Q_pruned_repeats.csv?dl=0

这是我的代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from astropy.table import Table
from astropy.io import ascii
from astropy.io import fits

filename = 'DR14Q_pruned_repeats.csv'
df = pd.read_csv(filename)

multiples = df[df["N_SPEC"] >2]

multiples.plot.scatter(x='MJD', y='N_SPEC')
plt.show()

multiples.plot.scatter(x='MJD', y='MJD_DUPLICATE')
plt.show()

MJD与MJD_DUPLICATE绘图线返回错误::

ValueError: scatter requires y column to be numeric

并且pd.to_numeric行返回只是 NaNs。

1 个答案:

答案 0 :(得分:0)

你需要:

import ast

doubles   = df[df["N_SPEC"] ==2].copy()
multiples = df[df["N_SPEC"] >2].copy()
repeats   = df[df["N_SPEC"] >1].copy()

multiples.plot.scatter(x='MJD', y='N_SPEC')
plt.show()

将列MJD_DUPLICATE转换为string s的元组,然后按位置选择值 - 例如str[1]表示元组的第二个值:

print (multiples['MJD_DUPLICATE'].head(10))
5      (0, 56279, 0, 56539, 0, 56957, -1, -1, -1, -1,...
85     (0, 56243, 0, 56543, 0, 57328, -1, -1, -1, -1,...
170    (0, 52262, 0, 55447, 0, 57011, -1, -1, -1, -1,...
200    (0, 52262, 0, 55443, 0, 57006, -1, -1, -1, -1,...
262    (0, 52525, 0, 55443, 0, 57011, -1, -1, -1, -1,...
277    (0, 51793, 0, 55531, 0, 57006, -1, -1, -1, -1,...
287    (0, 55182, 0, 55184, 0, 55443, -1, -1, -1, -1,...
313    (0, 56248, 0, 56245, 0, 56572, -1, -1, -1, -1,...
314    (0, 55182, 0, 55184, 0, 55444, -1, -1, -1, -1,...
324    (0, 52261, 0, 55184, 0, 55444, -1, -1, -1, -1,...
Name: MJD_DUPLICATE, dtype: object

ser = multiples['MJD_DUPLICATE'].apply(ast.literal_eval).str[1]
multiples['MJD_DUPLICATE'] = pd.to_numeric(ser, errors='coerce')

print (multiples['MJD_DUPLICATE'].head(10)) 
5      56279
85     56243
170    52262
200    52262
262    52525
277    51793
287    55182
313    56248
314    55182
324    52261
Name: MJD_DUPLICATE, dtype: int64

multiples.plot.scatter(x='MJD', y='MJD_DUPLICATE')
plt.show()