我有以下DataFrame
| name | number | value |
|------|--------|-------|
| a | 1 | 13 |
| a | 2 | 18 |
| a | 3 | 54 |
| b | 1 | 1 |
| c | 1 | 135 |
| c | 2 | 153 |
| c | 3 | 512 |
| d | 1 | 36 |
| d | 2 | 74 |
| d | 3 | 209 |
| e | 1 | 108 |
| e | 2 | 150 |
| e | 3 | 339 |
| f | 1 | 27 |
| f | 2 | 41 |
| f | 3 | 177 |
| g | 1 | 102 |
| g | 2 | 102 |
| g | 3 | 360 |
| h | 1 | 1 |
| i | 1 | 1 |
我希望做两件事......
答案 0 :(得分:1)
您要求进行大量格式化。但这是一个简单的例子:
import io
import pandas as pd
import matplotlib.pyplot as plt
string = u"""number,name,value
a,1,13
a,2,15
a,3,18
b,1,1
c,1,17
c,2,21
"""
df = pd.read_csv(io.StringIO(string))
# Remove uniques with boolean indexing
df = df[df.duplicated('number',keep=False)]
#https://stackoverflow.com/questions/41494942/pandas-dataframe-groupby-plot
df.set_index('name', inplace=True)
df.groupby('number')['value'].plot(legend=True)
plt.show()
答案 1 :(得分:0)
旋转DataFrame并绘制
df[['number', 'value']] = df[['number', 'value']].astype(int)
name_cnt = df.groupby('name').size()
required_nm = name_cnt[ name_cnt != 1].index
required_rows = df.loc[df.name.isin(required_nm)] # select non repeating row in 'name' columns
required_rows.pivot(columns='name', index='number', values='value').plot()