我有python pandas数据帧,其中一列包含月份名称.
如何使用字典进行自定义排序,例如:
custom_dict = {'March':0, 'April':1, 'Dec':3}
Run Code Online (Sandbox Code Playgroud) 我正在尝试pandas使用简单的函数来就地更改我的一个列.
在阅读完整个Dataframe之后,我尝试在一个Serie上应用函数:
wanted_data.age.apply(lambda x: x+1)
Run Code Online (Sandbox Code Playgroud)
它工作得很好.当我尝试将其放回我的DataFrame时,唯一的问题是:
wanted_data.age = wanted_data.age.apply(lambda x: x+1)
Run Code Online (Sandbox Code Playgroud)
要么:
wanted_data['age'] = wanted_data.age.apply(lambda x: x+1)
Run Code Online (Sandbox Code Playgroud)
抛出以下警告:
> C:\Anaconda\lib\site-packages\pandas\core\generic.py:1974:
> SettingWithCopyWarning: A value is trying to be set on a copy of a
> slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =
> value instead
>
> See the the caveats in the documentation:
> http://pandas.pydata.org/pandas-docs/stable
> /indexing.html#indexing-view-versus-copy self[name] = value
Run Code Online (Sandbox Code Playgroud)
当然,我可以使用以下的长形式设置DataFrame:
wanted_data.loc[:, 'age'] = wanted_data.age.apply(lambda x: x+1)
Run Code Online (Sandbox Code Playgroud)
但是,没有其他更容易和更语法更好的方法吗?
谢谢!
我必须在python中清理输入数据文件.由于拼写错误,数据字段可能包含字符串而不是数字.我想识别所有字符串,并使用pandas用NaN填充这些字段.另外,我想记录这些字段的索引.
最原始的方法之一是循环遍历每个字段并检查它是否是数字,但如果数据很大,则会耗费大量时间.
我的csv文件包含类似于下表的数据:
Country Count Sales
USA 1 65000
UK 3 4000
IND 8 g
SPA 3 9000
NTH 5 80000
Run Code Online (Sandbox Code Playgroud)
....假设我在数据中有60,000个这样的行.
理想情况下,我想确定IND行在SALES列下的值无效.有关如何有效地做到这一点的任何建议?
我有一个python数据帧df,有五列五行.我想获得最多三个值的行和列名称
例:
df =
A B C D E F
1 00 01 02 03 04 05
2 06 07 08 09 10 11
3 12 13 14 15 16 17
4 18 19 20 21 22 23
5 24 25 26 27 28 29
Run Code Online (Sandbox Code Playgroud)
输出显示类似[5,F],[5,E],[5,D]的内容
我在其中有以下熊猫数据框NaN。
import pandas as pd
df = pd.DataFrame([1,2,3,float('nan')], columns=['A'])
df
A
0 1
1 2
2 3
3 NaN
Run Code Online (Sandbox Code Playgroud)
我还有filter_list要用来过滤数据框的列表。但是,如果我使用.isin()功能,它不会检测到NaN。而不是让True我进入False最后一行
filter_list = [1, float('nan')]
df['A'].isin(filter_list)
0 True
1 False
2 False
3 False
Name: A, dtype: bool
Run Code Online (Sandbox Code Playgroud)
预期产量:
0 True
1 False
2 False
3 True
Name: A, dtype: bool
Run Code Online (Sandbox Code Playgroud)
我知道我可以.isnull()用来检查NaNs。但是在这里,我还需要检查其他值。我正在使用熊猫0.16.0版
编辑:列表filter_list来自用户。所以它可能有也可能没有NaN。那就是为什么我正在使用.isin()
我有一个熊猫系列,里面有很多文字。使用包中的CountVectorizer函数 sklearn,我计算了稀疏矩阵。我也确定了最重要的词。现在我只想过滤那些最重要的词的稀疏矩阵。
原始数据包含多个7000行并且包含多个75000单词。因此我在这里创建了一个示例数据
from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd
words = pd.Series(['This is first row of the text column',
'This is second row of the text column',
'This is third row of the text column',
'This is fourth row of the text column',
'This is fifth row of the text column'])
count_vec = CountVectorizer(stop_words='english')
sparse_matrix = count_vec.fit_transform(words)
Run Code Online (Sandbox Code Playgroud)
我已经为该列中的所有单词创建了稀疏矩阵。这里只是为了打印我的稀疏矩阵,我正在使用.toarray()函数将其转换为数组。
print count_vec.get_feature_names()
print sparse_matrix.toarray()
[u'column', u'fifth', u'fourth', u'row', u'second', u'text']
[[1 …Run Code Online (Sandbox Code Playgroud) 我想将一个函数应用于a的每个元素numpy.ndarray,如下所示:
import numpy
import math
a = numpy.arange(10).reshape(2,5)
b = map(math.sin, a)
print b
Run Code Online (Sandbox Code Playgroud)
但这给了:
TypeError: only length-1 arrays can be converted to Python scalars
Run Code Online (Sandbox Code Playgroud)
我知道我可以这样做:
import numpy
import math
a = numpy.arange(10).reshape(2,5)
def recursive_map(function, value):
if isinstance(value, (list, numpy.ndarray)):
out = numpy.array(map(lambda x: recursive_map(function, x), value))
else:
out = function(value)
return out
c = recursive_map(math.sin, a)
print c
Run Code Online (Sandbox Code Playgroud)
我的问题是:是否有内置函数或方法来执行此操作?它似乎很简单,但我找不到它.我在用Python 2.7.
目前在我的 Tornado 应用程序中,我PeriodicCallback每隔一小时定期调用一次回调。像这样:
import tornado.ioloop
from tornado.ioloop import PeriodicCallback
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=urls,
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
# Here i have option to specify interval, but how to specify when to start?
daily_alerts = PeriodicCallback(lambda: send_email.daily_alert(), 3600000)
daily_alerts.start()
tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)
在这里,我可以选择设置间隔时间 ( 3600000),但如何指定此定期回调应何时开始?