【发布时间】:2020-02-03 22:37:31
【问题描述】:
我有这段代码,它获取一个 csv 文件,按列过滤,然后绘制另一列的值。
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
from matplotlib import pyplot as plt
import numpy as np
df = pd.read_csv(r'C:/Desktop/Plot/dataframe.csv', delimiter=";", encoding='unicode_escape')
df['num_1'] = df['num_2'].str.split(',').str[0]
df['num_1'] = df['num_2'].astype('int64', copy=False)
X=df[df['Describe']=='The Start of Journey']['num_2'].values
dev_x= X
# Set figure size
plt.figure(figsize=(10, 5))
plt.hist(dev_x, bins=5)
plt.title('Data')
这是数据集
+-----+-------+-----------------------+--------+--------+
| | name | Describe | num_1 | num_2 |
+-----+-------+-----------------------+--------+--------+
| 0 | er | The Start of Journey | 17 | 249,5 |
| 1 | NaN | NaN | 58 | 51,0 |
| 2 | NaN | NaN | 14 | 66,5 |
| 3 | NaN | NaN | 526 | 84,0 |
| 4 | be | The end of journey | 3 | 13,0 |
| 5 | tg | Levels | 342 | 34,0 |
| 6 | NaN | NaN | 231 | 55,6 |
| 7 | NaN | NaN | 23 | 75,0 |
| 8 | tf | counts | 54 | 34,6 |
| 9 | sf | The Start of Journey | 52 | 4324,0 |
| 10 | gd | The Start of Journey | 352 | 54.0 |
+-----+-------+-----------------------+--------+--------+
我想修改代码,使其执行以下操作
- 提示用户添加 csv 文件
- 提示用户添加我们要过滤的列的名称(本例为
Describe列) - 提示用户添加字符串(本例为
The Start of Journey) - 提示用户添加我们要绘制数据的列的名称(本例为
num_2)
我已经检查了其他来源,但由于代码结构的原因,我在这方面遇到了麻烦。
【问题讨论】:
-
如果您在浏览器中搜索“Python 用户输入”,您会找到比我们在此处管理的更能解释这一点的参考资料。
-
同样投票关闭,读入输入实际上是一行代码,你可以从谷歌的第一个结果中找到,其余的都在使用 pandas API。去玩吧,如果你卡在某个特定的事情上,回来一定会有人帮忙的。
-
您的问题是什么?您只是要求人们编写代码来满足您的要求吗?