在Pandas中绘制多列(将字符串转换为浮点数) [英] Plotting a multiple column in Pandas (converting strings to floats)

查看:1011
本文介绍了在Pandas中绘制多列(将字符串转换为浮点数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此处给出的数据,我想绘制"MJD"与"MULTIPLE_MJD"的关系图: https://www.dropbox.com/s/cicgc1eiwrz93tg/DR14Q_prunedsseveral3col. csv?dl = 0

I'd like to plot "MJD" vs "MULTIPLE_MJD" for the data given here:: 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值::

This makes a plot, but only for one value of MJD_DUPLICATE::

print(df['MJD_DUPLICATE_NEW'])

0 55214 1 55209 ...

0 55214 1 55209 ...

想法?

推荐答案

这里有两个问题:

  1. 告诉熊猫在CSV中解析元组.在此处进行了介绍:从带有熊猫的csv文件中读取元组
  2. 将元组转换为多行.在此处进行介绍:在Dafaframe中将元组分成多行
  1. Telling Pandas to parse tuples within the CSV. This is covered here: Reading back tuples from a csv file with pandas
  2. Transforming the tuples into multiple rows. This is covered here: Getting a tuple in a Dafaframe into multiple rows

将它们放在一起,这是解决问题的一种方法:

Putting those together, here is one way to solve your problem:

# 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')

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

If you want to remove the 0 and -1 values, a mask will work:

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

这篇关于在Pandas中绘制多列(将字符串转换为浮点数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆