我有以下内容DataFrame:
from pandas import *
df = DataFrame({'foo':['a','b','c'], 'bar':[1, 2, 3]})
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:
bar foo
0 1 a
1 2 b
2 3 c
Run Code Online (Sandbox Code Playgroud)
现在我希望有类似的东西:
bar
0 1 is a
1 2 is b
2 3 is c
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我尝试了以下方法:
df['foo'] = '%s is %s' % (df['bar'], df['foo'])
Run Code Online (Sandbox Code Playgroud)
但它给了我一个错误的结果:
>>>print df.ix[0]
bar a
foo 0 a
1 b
2 c
Name: bar is 0 1
1 2
2
Name: 0
Run Code Online (Sandbox Code Playgroud)
抱歉有一个愚蠢的问题,但是这只熊猫:在DataFrame中合并两列对我没用.
我有一个包含三个字符串列的数据框.我知道第3列中唯一的一个值对前两个的每个组合都有效.要清理数据,我必须按数据框前两列进行分组,并为每个组合选择第三列的最常见值.
我的代码:
import pandas as pd
from scipy import stats
source = pd.DataFrame({'Country' : ['USA', 'USA', 'Russia','USA'],
'City' : ['New-York', 'New-York', 'Sankt-Petersburg', 'New-York'],
'Short name' : ['NY','New','Spb','NY']})
print source.groupby(['Country','City']).agg(lambda x: stats.mode(x['Short name'])[0])
Run Code Online (Sandbox Code Playgroud)
最后一行代码不起作用,它说"键错误'短名称'",如果我尝试仅按城市分组,那么我得到一个AssertionError.我该怎么办呢?
如果您来到这里寻找有关如何合并索引
DataFrame和Series索引的信息,请查看此答案.OP的初衷是询问如何将系列元素作为列分配给另一个DataFrame.如果您有兴趣知道答案,请查看EdChum 接受的答案.
我能想到的最好的是
df = pd.DataFrame({'a':[1, 2], 'b':[3, 4]}) # see EDIT below
s = pd.Series({'s1':5, 's2':6})
for name in s.index:
df[name] = s[name]
a b s1 s2
0 1 3 5 6
1 2 4 5 6
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议更好的语法/更快的方法?
我的尝试:
df.merge(s)
AttributeError: 'Series' object has no attribute 'columns'
Run Code Online (Sandbox Code Playgroud)
和
df.join(s)
ValueError: Other Series must have a name
Run Code Online (Sandbox Code Playgroud)
编辑前两个答案突出显示了我的问题的问题,所以请使用以下内容来构建df:
df = pd.DataFrame({'a':[np.nan, 2, 3], 'b':[4, 5, …Run Code Online (Sandbox Code Playgroud) 我想,我无法在这里看到更大的图景; 但基本上我不知道为什么你会使用os.path.join而不是正常的字符串连接?
我主要使用VBScript,所以我不明白这个功能的意义.
我有一个像这样的pandas数据框:
admit gpa gre rank
0 3.61 380 3
1 3.67 660 3
1 3.19 640 4
0 2.93 520 4
Run Code Online (Sandbox Code Playgroud)
现在我想获得pandas中的行列表,如:
[[0,3.61,380,3], [1,3.67,660,3], [1,3.19,640,4], [0,2.93,520,4]]
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
以下代码不起作用.
import pandas as pd
import numpy as np
df=pd.DataFrame(['ONE','Two', np.nan],columns=['x'])
xLower = df["x"].map(lambda x: x.lower())
Run Code Online (Sandbox Code Playgroud)
如何调整它以获得xLower = ['one','two',np.nan]?效率很重要,因为真正的数据框架是巨大的.
我最近需要安装一些软件包
pip install future
pip install scikit-learn
pip install numpy
pip install scipy
Run Code Online (Sandbox Code Playgroud)
我也尝试过sudo在他们面前写过但是所有这些都是红线中出现以下错误:
Exception:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 290, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1198, in prepare_files
do_download,
File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1376, in unpack_url
self.session,
File "/usr/lib/python2.7/dist-packages/pip/download.py", line 572, in unpack_http_url
download_hash = _download_url(resp, link, temp_location)
File "/usr/lib/python2.7/dist-packages/pip/download.py", line 433, in _download_url
for chunk in resp_read(4096):
File "/usr/lib/python2.7/dist-packages/pip/download.py", line 421, …Run Code Online (Sandbox Code Playgroud) 我有一个列的数据帧A,B.我需要C为每个记录/行创建一个列:
C = max(A, B).
我应该怎么做呢?
谢谢.
我想为_x每个列名添加后缀,如下所示:
featuresA = myPandasDataFrame.columns.values + '_x'
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?另外,如果我想添加x_后缀,解决方案将如何变化?
此代码来自Python的文档.我有点困惑.
words = ['cat', 'window', 'defenestrate']
for w in words[:]:
if len(w) > 6:
words.insert(0, w)
print(words)
Run Code Online (Sandbox Code Playgroud)
以下是我最初的想法:
words = ['cat', 'window', 'defenestrate']
for w in words:
if len(w) > 6:
words.insert(0, w)
print(words)
Run Code Online (Sandbox Code Playgroud)
为什么这段代码创建了一个无限循环而第一个没有?