我知道将函数作为组键传递每个索引值调用一次函数,返回值用作组名.我无法弄清楚的是如何在列值上调用函数.
所以我可以这样做:
people = pd.DataFrame(np.random.randn(5, 5),
columns=['a', 'b', 'c', 'd', 'e'],
index=['Joe', 'Steve', 'Wes', 'Jim', 'Travis'])
def GroupFunc(x):
if len(x) > 3:
return 'Group1'
else:
return 'Group2'
people.groupby(GroupFunc).sum()
Run Code Online (Sandbox Code Playgroud)
这将数据分成两组,其中一组的索引值为3或更小,另一组的长度为3或更多.但是我如何传递其中一个列值?因此,例如,如果每个索引点的列d值大于1.我意识到我可以执行以下操作:
people.groupby(people.a > 1).sum()
Run Code Online (Sandbox Code Playgroud)
但我想知道如何在用户定义的函数中执行此操作以供将来参考.
就像是:
def GroupColFunc(x):
if x > 1:
return 'Group1'
else:
return 'Group2'
Run Code Online (Sandbox Code Playgroud)
但我怎么称呼这个?我试过了
people.groupby(GroupColFunc(people.a))
Run Code Online (Sandbox Code Playgroud)
和类似的变体,但这不起作用.
如何将列值传递给函数?我如何传递多个列值,例如分组是否people.a> people.b?
我正在使用我已经在我的工作中使用了很长时间的聚合函数.这个想法是,如果系列传递给函数的长度为1(即该组只有一个观察值),则返回该观察值.如果传递的系列的长度大于1,则在列表中返回观察结果.
这对某些人来说可能看起来很奇怪,但这不是X,Y问题,我有充分的理由想要这样做与这个问题无关.
这是我一直在使用的功能:
def MakeList(x):
""" This function is used to aggregate data that needs to be kept distinc within multi day
observations for later use and transformation. It makes a list of the data and if the list is of length 1
then there is only one line/day observation in that group so the single element of the list is returned.
If the list is longer than one then there are multiple line/day observations and the list itself is …
Run Code Online (Sandbox Code Playgroud) 如何将IPython笔记本转换为HTML以便在Google Blogger博客中使用?
这里有一个答案:
我已经阅读了fperez的相关博客,但-f blogger-html
似乎nbconvert
不再是一个选项了(是吗?).
我可以将笔记本转换为HTML,但谷歌似乎并不喜欢这样.如果我使用该--template basic
选项,标题将消失,并且数学表达式的LaTex渲染将丢失.此外,Google似乎在保存/发布时会产生错误,因此无法保存.
任何人都有关于更新过程的任何信息?
我有一个看起来像这样的文本输入小部件:
<ReaderWidget>:
Label:
text: 'Please scan EBT card'
font_size: root.height/8
size: self.texture_size
bold: True
color: 0, 0.70, 0.93, 1
TextInput:
focus: True
password: True
multiline: False
cursor: 0, 0
Run Code Online (Sandbox Code Playgroud)
根据用户按下另一个小部件中的按钮,小部件会动态添加到布局中。目前,用户必须在输入文本之前将鼠标/手指指向文本框,我希望光标在文本框中准备好接收文本,而用户不必通过鼠标按下来指示。有没有办法做到这一点?
好像focus : True
应该这样做。但似乎没有。
我在将csv文件读入pandas数据框时遇到了困难.我是大熊猫的全新人,这阻碍了我的进步.我已经阅读了文档并搜索了解决方案,但我无法继续.我试过以下无济于事......
import pandas as pd
import numpy as np
pd.read_csv('C:\Users\rcreedon\Desktop\TEST.csv')
pd.read_csv("C:\Users\rcreedon\Desktop\TEST.csv")
Run Code Online (Sandbox Code Playgroud)
和带/不带引号的类似排列.
它吐出一个大的复合错误,结尾于:
IOError: File C:\Users
creedon\Desktop\TEST.csv does not exist
Run Code Online (Sandbox Code Playgroud)
奇怪的是,在错误中它错过了"rcreedon"中的"r".这是导致问题的原因吗?
只是为了它,我也尝试过
pd.read_csv('C:\rcreedon\Desktop\TEST.csv')
Run Code Online (Sandbox Code Playgroud)
当错误被返回时,错过了'r'.
很抱歉这是一个块头,但我在这里挣扎....
任何帮助赞赏.
我喜欢np.where,但从未完全掌握它.
我有一个数据帧让我们说它看起来像这样:
import pandas as pd
import numpy as np
from numpy import nan as NA
DF = pd.DataFrame({'a' : [ 3, 0, 1, 0, 1, 14, 2, 0, 0, 0, 0],
'b' : [ 3, 0, 1, 0, 1, 14, 2, 0, 0, 0, 0],
'c' : [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'd' : [5, 1, 2 ,1, 1 ,22, 30, 1, 0, 0, 0]})
Run Code Online (Sandbox Code Playgroud)
现在我要做的是在所有行值为零时用NaN值替换0值.关键是我想在所有行值都不为零的情况下维护行中的其他任何值.
我想做这样的事情:
cols = ['a', 'b', 'c', …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 matplotlib。当网格形状已经存在时,我很难理解如何添加子图。
例如,如果我创建一个带有 2*2 子图网格的图形,我如何添加第 5 个或第 6 个。即如何更改几何形状以适应另一个子图。如果我这样做:
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True, facecolor='red', \
dpi = 100, edgecolor = 'red', linewidth = 5.0, frameon = True)
ax1.plot(x, y)
ax1.set_title('Sharing x per column, y per row')
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')
Run Code Online (Sandbox Code Playgroud)
然后我想在下面添加另一个子图:
f.add_subplot(3, 2, 5)
Run Code Online (Sandbox Code Playgroud)
然后新的子图与第四个重叠,当我希望它明显位于下方时。我需要改变几何吗?如果是这样,如何?还是只是立场问题?
更一般地说,带有子情节的 …
据我所知,当您使用 DataFrame 列调用 groupby.transform 时,该列将传递给转换数据的函数。但我无法理解的是如何将多个列传递给函数。
people = DataFrame(np.random.randn(5, 5), columns=['a', 'b', 'c', 'd', 'e'], index=['Joe', 'Steve', 'Wes', 'Jim', 'Travis'])
key = ['one', 'two', 'one', 'two', 'one']
Run Code Online (Sandbox Code Playgroud)
现在我可以轻松地贬低数据等,但我似乎无法正确执行的是使用多个列值作为函数的参数来转换组内的数据。例如,如果我想为每个观察添加一个采用值 a.mean() - b.mean() * c 的列“f”,如何使用转换方法来实现这一点。
我尝试过以下变体
people['f'] = float(NA)
Grouped = people.groupby(key)
def TransFunc(col1, col2, col3):
return col1.mean() - col2.mean() * col3
Grouped.f.transform(TransFunc(Grouped['a'], Grouped['b'], Grouped['c']))
Run Code Online (Sandbox Code Playgroud)
但这显然是错误的。我还尝试将函数包装在lambda 中,但也无法完全使其工作。
我能够通过以下方式迭代组来实现结果:
for group in Grouped:
Amean = np.mean(list(group[1].a))
Bmean = np.mean(list(group[1].b))
CList = list(group[1].c)
IList = list(group[1].index)
for y in xrange(len(CList)):
people['f'][IList[y]] = (Amean - …
Run Code Online (Sandbox Code Playgroud) 我最近已经意识到链式赋值的危险,我正在尝试使用loc [rowindex,colindex]在pandas中使用正确的索引方法.我正在使用混合数据类型(混合在同一系列的np.float64和列表和字符串中) - 这是不可避免的.我有一个整数索引
我正在通过数据框运行以下循环
Count = 0
for row in DF.index:
print row
if '/' in str(DF.order_no[row]) and '/' not in str(DF.buyer[row]) and '/' not in str(DF.buyer[row])\
and '/' not in str(DF.smv[row]) and '/' not in str(DF.item[row]):
DF.loc[row, 'order_no'] = str(DF.loc[row, 'order_no']).split('/')
Count +=1
Run Code Online (Sandbox Code Playgroud)
计数
哪个返回错误:
TypeError: object of type 'int' has no len()
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
在那个循环中,我可以做到:
print DF.loc[row, 'order_no']
Run Code Online (Sandbox Code Playgroud)
和
print DF.loc[row, 'order_no'] == str(DF.loc[row, order_no]).split('/')
Run Code Online (Sandbox Code Playgroud)
但不是
DF.loc[row, 'order_no'] = str(DF.loc[row, order_no]).split('/')
Run Code Online (Sandbox Code Playgroud)
使用print语句我看到它在第3行卡住了,但是:
DF.loc[3, 'order_no']
Run Code Online (Sandbox Code Playgroud)
工作得很好.
帮助升值.
编辑
解决方法如下: …
我认为这是编程 101,但我错过的课程:
我有一个类,其中将大约 50 个默认参数传递给init。然后,用户可以在构造时为这些参数提供不同的值,或者他们可以以正常方式修改结果属性。
我想做的是创建一个函数,可能在该类之外,允许用户创建该类的多个版本,然后返回有用的信息。但是,函数中类的每次迭代都会为构造函数提供不同的参数。
如何最好地允许函数的用户向传递给类构造函数的函数提供参数。
这是我想要实现的目标:
class someClass(object):
def __init__(self, a=None, b=None, c=None, d=None, e=None):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
def some_method(self):
# do something
return # something useful
simulations = {'1': {'a':3, 'e':6},
'2': {'b':2, 'c':1}}
def func(simulations=simulations):
results = []
for sim in simulations.keys():
sc = someClass(simulations[sim]) # use the arguments in the dict to pass to constructor
results.append(sc.some_method())
return results
Run Code Online (Sandbox Code Playgroud) 请帮助这让我发疯。我正在使用 Ipython Shell,并拼命尝试接收不那么冗长的错误消息。对于每个小错误,我都会收到 40 多行。我认为我需要做的是将 xmode 设置为普通。我起初尝试在启动 ipython 时在命令提示符下执行此操作,但我认为 xmode 是一个魔术函数,因此应该在 Ipython shell 中调用,因此我不会在命令提示符中粘贴我尝试的内容。在外壳中,我尝试过:
xmode('Plain')
xmode('plain')
xmode(plain)
xmode(Plain)
Run Code Online (Sandbox Code Playgroud)
以上所有内容都带有 % 前缀。我也试过
xmode<('Plain')>
Run Code Online (Sandbox Code Playgroud)
以及大约十亿种其他排列。
有人可以告诉我我做错了什么......?
非常感谢