我在csv文件中有这样的数据
Symbol,Action,Year
AAPL,Buy,2001
AAPL,Buy,2001
BAC,Sell,2002
BAC,Sell,2002
Run Code Online (Sandbox Code Playgroud)
我能够像这样阅读它和groupby
df.groupby(['Symbol','Year']).count()
Run Code Online (Sandbox Code Playgroud)
我明白了
Action
Symbol Year
AAPL 2001 2
BAC 2002 2
Run Code Online (Sandbox Code Playgroud)
我希望这个(顺序无关紧要)
Action
Symbol Year
AAPL 2001 2
AAPL 2002 0
BAC 2001 0
BAC 2002 2
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能计算零出现率
我想检查数据帧中是否存在行,以下是我的代码:
df = pd.read_csv('dbo.Access_Stat_all.csv',error_bad_lines=False, usecols=['Name','Format','Resource_ID','Number'])
df1 = df[df['Resource_ID'] == 30957]
df1 = df1[['Format','Name','Number']]
df1 = df1.groupby(['Format','Name'], as_index=True).last()
pd.options.display.float_format = '{:,.0f}'.format
df1 = df1.unstack()
df1.columns = df1.columns.droplevel()
if 'entry' in df1:
df2 = df1[1:4].sum(axis=0)
else:
df2 = df1[0:3].sum(axis=0)
df2.name = 'sum'
df2 = df1.append(df2)
print(df2)
Run Code Online (Sandbox Code Playgroud)
这是输出:
Name Apr 2013 Apr 2014 Apr 2015 Apr 2016 Apr 2017 Aug 2010 Aug 2013
Format
entry 0 0 0 1 4 1 0
pdf 13 12 4 23 7 1 9
sum 13 12 …
Run Code Online (Sandbox Code Playgroud) 假设我有一个如下的数据框.我想要的是,如果列a,b,c之间的数字出现最多,那么它应输出该数字,或者如果所有三个数字不同,则取a的数字.例如,在第一行中,1出现在1和5中最多,然后d中的输出为1.但在第二行中,列a,b,c的所有三个数字11,2,7都不同,输出是列的值a(11),因此d中的输出为11
list a b c
1 1 5 1
11 11 2 7
0 0 0 0
9 5 9 5
8 8 2 7
Run Code Online (Sandbox Code Playgroud)
预期产出
list a b c d
1 1 5 1 1
11 11 2 7 11
0 0 0 0 0
9 5 9 5 5
8 8 2 7 8
Run Code Online (Sandbox Code Playgroud) 如何在熊猫的数据框中迭代几天?
例:
我的数据框:
time consumption
time
2016-10-17 09:00:00 2016-10-17 09:00:00 2754.483333
2016-10-17 10:00:00 2016-10-17 10:00:00 2135.966666
2016-10-17 11:00:00 2016-10-17 11:00:00 1497.716666
2016-10-17 12:00:00 2016-10-17 12:00:00 448.100000
2016-10-24 09:00:00 2016-10-24 09:00:00 1527.716666
2016-10-24 10:00:00 2016-10-24 10:00:00 1219.833333
2016-10-24 11:00:00 2016-10-24 11:00:00 1284.350000
2016-10-24 12:00:00 2016-10-24 12:00:00 14195.633333
2016-10-31 09:00:00 2016-10-31 09:00:00 2120.933333
2016-10-31 10:00:00 2016-10-31 10:00:00 1630.700000
2016-10-31 11:00:00 2016-10-31 11:00:00 1241.866666
2016-10-31 12:00:00 2016-10-31 12:00:00 1156.266666
Run Code Online (Sandbox Code Playgroud)
伪代码:
for day in df:
print day
Run Code Online (Sandbox Code Playgroud)
第一次迭代返回:
time consumption
time
2016-10-17 09:00:00 2016-10-17 …
Run Code Online (Sandbox Code Playgroud) 我想从数据框中获取每一行的某个部分的第二高值。我该怎么做呢?
我已经尝试了以下代码,但它不起作用:
df.iloc[:, 5:-3].nlargest(2)(axis=1, level=2)
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以获得这个?
我是我的第一个“真正的”机器学习算法的新手。抱歉,如果这是重复的,但我在 SO 上找不到答案。
我有以下数据框(df
):
index Feature1 Feature2 Feature3 Target
001 01 01 03 0
002 03 03 01 1
003 03 02 02 1
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
data = df[['Feature1', 'Feature2', 'Feature3']]
labels = df['Target']
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size = 0.8)
clf = RandomForestClassifier().fit(X_train, y_train)
prediction_of_probability = clf.predict_proba(X_test)
Run Code Online (Sandbox Code Playgroud)
我正在苦苦挣扎的是如何才能'prediction_of_probability'
回到数据框df
?
我知道预测不会适用于原始数据框中的所有项目。
预先感谢您帮助像我这样的新手!
我遇到了下面的代码行,当其中不存在“.index”时会出现错误。
print(df.drop(df[df['Quantity'] == 0].index).rename(columns={'Weight': 'Weight (oz.)'}))
Run Code Online (Sandbox Code Playgroud)
在熊猫中使用 drop 时“.index”的目的是什么?
按照官方文档的这个例子,我可以在不同的页面中创建一个包含我想要的图的pdf文件.但是我想在页面中添加一些文本(不在图中)并且我已经尝试过这种方式而没有成功:
with PdfPages('multipage_pdf.pdf') as pdf:
fig = plt.figure(figsize=(11.69,8.27))
x = df1.index
y1 = df1[col1]
y2 = df1[col2]
plt.plot(x, y1, label=col1)
plt.plot(x, y2, label=col2)
plt.legend(loc='best')
plt.grid(True)
plt.title('Title')
txt = 'this is an example'
plt.text(1,1,txt)
pdf.savefig()
plt.close()
Run Code Online (Sandbox Code Playgroud)
我怎样才能显示文字this is an example
?是否可以创建仅包含文本的第一页?提前致谢
我有以下数据框,我试图绘制它,以便它在x轴显示8-19的索引数据.
如果我df.plot()
没有显示任何标签.如果我这样做df.plot(use_index=True)
,行为就没有改变.最后我尝试了df.plot(xticks=df.index)
但是我收到了一个错误AttributeError: 'NoneType' object has no attribute 'seq'
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
null = np.nan
df = pd.DataFrame.from_dict({"today sensor 1": {"08": 22.9, "09": 22.7, "10": 22.8, "11": 23.6, "12": 24.1, "13": 24.9,
"14": 25.0, "15": 25.2, "16": 25.7, "17": 26.1, "18": 26.0, "19": 25.8},
"today sensor 2": {"08": 24.5, "09": 24.5, "10": 24.8, "11": 25.3, "12": 26.4, "13": 26.7,
"14": 27.1, "15": 27.6, "16": 28.0, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用seaborn 在热图中绘制离散值。这是我试图绘制的列表:
xa = [[5, 4, 4, 4, 13, 4, 4],
[1, 9, 4, 3, 9, 1, 4],
[4, 1, 7, 1, 5, 3, 7],
[1, 9, 4, 3, 9, 5, 4],
[2, 1, 4, 1, 9, 4, 3],
[9, 4, 8, 1, 7, 1, 9],
[4, 8, 1, 7, 1, 4, 8]]
Run Code Online (Sandbox Code Playgroud)
这是我用来绘制热图的代码:
xa = [[5, 4, 4, 4, 13, 4, 4],
[1, 9, 4, 3, 9, 1, 4],
[4, 1, 7, 1, 5, 3, 7],
[1, 9, 4, …
Run Code Online (Sandbox Code Playgroud) python ×10
pandas ×8
matplotlib ×3
dataframe ×2
python-3.x ×2
data-science ×1
group-by ×1
pdf ×1
pdfpages ×1
seaborn ×1
text ×1