在Pandas中绘制多列(将字符串转换为浮点数)

时间:2017-10-15 17:45:11

标签: python pandas type-conversion series

我想为此处给出的数据绘制“MJD”与“MULTIPLE_MJD”的关系:: https://www.dropbox.com/s/cicgc1eiwrz93tg/DR14Q_pruned_several3cols.csv?dl=0

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import ast

filename = 'DR14Q_pruned_several3cols.csv'
datafile= path+filename
df = pd.read_csv(datafile)

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

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

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

这是一个情节,但仅适用于MJD_DUPLICATE ::

的一个值
print(df['MJD_DUPLICATE_NEW'])

0 55214    1 55209    ...

思想??

1 个答案:

答案 0 :(得分:0)

这里有两个问题:

  1. 告诉Pandas解析CSV中的元组。这包括:Reading back tuples from a csv file with pandas
  2. 将元组转换为多行。这包括:Getting a tuple in a Dafaframe into multiple rows
  3. 将这些放在一起,这是解决问题的一种方法:

    # Following https://stackoverflow.com/questions/23661583/reading-back-tuples-from-a-csv-file-with-pandas
    import pandas as pd
    import ast
    df = pd.read_csv("DR14Q_pruned_several3cols.csv",
                     converters={"MJD_DUPLICATE": ast.literal_eval})
    
    # Following https://stackoverflow.com/questions/39790830/getting-a-tuple-in-a-dafaframe-into-multiple-rows
    df2 = pd.DataFrame(df.MJD_DUPLICATE.tolist(), index=df.MJD)
    df3 = df2.stack().reset_index(level=1, drop=True)
    
    # Now just plot!
    df3.plot(marker='.', linestyle='none')
    

    enter image description here

    如果要删除0和-1值,掩码将起作用:

    df3[df3 > 0].plot(marker='.', linestyle='none')
    

    enter image description here