我有两个csv文件,每个文件都有一个列。
那个专栏分享了他们之间的信息,比如PassengerId,姓名,性别,年龄。等。
我正试图绘制一个图表框图,显示每个标题(先生、女士等)的乘客分布年龄。我得到一个错误。如何传递绘图的误差?
import csv as csv
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
csv_file_object = csv.reader(open('test.csv', 'r'))
header = next(csv_file_object)
data=[]
for row in csv_file_object:
data.append(row)
data = np.array(data)
csv_file_object1 = csv.reader(open('train.csv', 'r'))
header1 = next(csv_file_object1)
data1=[]
for row in csv_file_object:
data1.append(row)
data1 = np.array(data1)
Mergerd_file = header.merge(header1, on='PassengerId')
df = pd.DataFrame(Mergerd_file, index=['pAge', 'Tilte'])
df.T.boxplot(vert=False)
plt.subplots_adjust(left=0.25)
plt.show()
我得到错误这个错误
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-0d7fafc1fcf9> in <module>()
21
22
---> 23 Mergerd_file = header.merge(header1, on='PassengerId')
24
25 df = pd.DataFrame(Mergerd_file, index=['pAge', 'Tilte'])
AttributeError: 'list' object has no attribute 'merge'
最佳答案
我认为您需要先read_csv
,然后concat
同时DataFrames
和最后创建boxplot
:
df1 = pd.read_csv('el/test.csv')
print (df1.head())
df2 = pd.read_csv('el/train.csv')
print (df2.head())
df = pd.concat([df1, df2])
df['Title'] = df.Name.str.extract(', (.*)\.', expand=False)
print (df.head())
df[['Age','Title']].boxplot(vert=False, by='Title')
plt.subplots_adjust(left=0.25)
plt.show()
关于python - Pandas 盒图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41284251/