Python 按数据帧打印数据
我在绘制Pandas数据框时遇到一些问题,该数据框是从groupby()创建的,现在有一个RangeIndexPython 按数据帧打印数据,python,pandas,matplotlib,Python,Pandas,Matplotlib,我在绘制Pandas数据框时遇到一些问题,该数据框是从groupby()创建的,现在有一个RangeIndex plt.scatter(x=gb_df.A, y=gb_df.B) # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # ValueError: could not convert string to float: (89.1, 99] 例如,以下是我的输
plt.scatter(x=gb_df.A, y=gb_df.B)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# ValueError: could not convert string to float: (89.1, 99]
例如,以下是我的输入数据,共有四列:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df.head()
# A B C D
# 0 83 99 55 83
# 1 91 42 14 27
# 2 44 4 30 9
# 3 96 46 92 73
# 4 91 73 17 36
然后我应用groupby()得到两列:a和B的平均值
gb = df.groupby(pd.cut(df.A, 10)).B.mean()
gb
# A
# (-0.099, 9.9] 38.272727
# (9.9, 19.8] 49.800000
# (19.8, 29.7] 55.000000
# (29.7, 39.6] 50.454545
# (39.6, 49.5] 46.285714
# (49.5, 59.4] 44.800000
# (59.4, 69.3] 48.500000
# (69.3, 79.2] 55.615385
# (79.2, 89.1] 45.500000
# (89.1, 99] 51.866667
# Name: B, dtype: float64
gb_df = gb.to_frame().reset_index()
gb_df
# A B
# 0 (-0.099, 9.9] 38.272727
# 1 (9.9, 19.8] 49.800000
# 2 (19.8, 29.7] 55.000000
# 3 (29.7, 39.6] 50.454545
# 4 (39.6, 49.5] 46.285714
# 5 (49.5, 59.4] 44.800000
# 6 (59.4, 69.3] 48.500000
# 7 (69.3, 79.2] 55.615385
# 8 (79.2, 89.1] 45.500000
# 9 (89.1, 99] 51.866667
现在,当我尝试绘制A和B时,我得到一个错误,因为A列是RangeIndex
plt.scatter(x=gb_df.A, y=gb_df.B)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# ValueError: could not convert string to float: (89.1, 99]
感谢您的帮助。使用
left
获取leftbreak
gb_df['New_A']=gb_df.A.apply(lambda x : x.left).astype('float')
gb_df.plot.scatter(x = 'New_A', y='B')
数据信息:
gb_df
A B New_A
0 (-0.099, 9.9] 39.928571 -0.099
1 (9.9, 19.8] 33.090909 9.900
2 (19.8, 29.7] 41.900000 19.800
3 (29.7, 39.6] 46.500000 29.700
4 (39.6, 49.5] 52.454545 39.600
5 (49.5, 59.4] 37.866667 49.500
6 (59.4, 69.3] 60.600000 59.400
7 (69.3, 79.2] 71.300000 69.300
8 (79.2, 89.1] 42.714286 79.200
9 (89.1, 99.0] 52.545455 89.100
非常感谢。你是如何发现左
的用法的?我在Pandas文档页面上查看了RangeIndex
,但没有找到任何内容:@stackoverflowuser2010我以前遇到过同样的问题,然后我转到源代码,你会发现RangeIndex是按interval创建的,interval本身有左调用和右调用。;-)@stackoverflowuser2010,下面是文件