【python – Pandas,Pivot错误 – 无法使用null键标记索引】教程文章相关的互联网学习教程文章

Python pandas / matplotlib注释条形图列上方的标签【代码】

如何在条形图中的条形图上方添加值的标签:import pandas as pd import matplotlib.pyplot as pltdf=pd.DataFrame({'Users': [ 'Bob', 'Jim', 'Ted', 'Jesus', 'James'],'Score': [10,2,5,6,7],})df = df.set_index('Users') df.plot(kind='bar', title='Scores')plt.show()解决方法:捕获绘制绘图的轴,然后将其作为通常的matplotlib对象进行操作.将值高于条形图将是这样的:ax = df.plot(kind='bar', title='Scores') ax.set_yli...

python – 将多个csv文件读取到HDF5时的Pandas ParserError EOF字符【代码】

使用Python3,Pandas 0.12 我正在尝试将多个csv文件(总大小为7.9 GB)写入HDF5存储,以便稍后处理. csv文件每个包含大约一百万行,15列和数据类型主要是字符串,但有些浮点数.但是,当我尝试读取csv文件时,我收到以下错误:Traceback (most recent call last):File "filter-1.py", line 38, in <module>to_hdf()File "filter-1.py", line 31, in to_hdffor chunk in reader:File "C:\Python33\lib\site-packages\pandas\io\parsers.py", ...

python – matplotlib:在条形图上绘制多列pandas数据框【代码】

我使用以下代码绘制条形图:import matplotlib.pyplot as pls my_df.plot(x='my_timestampe', y='col_A', kind='bar') plt.show()情节很好.但是,我希望通过在列表中包含3列:’col_A’,’col_B’和’col_C’来改进图形.如下图所示:我希望col_A在x轴上方以蓝色显示,col_B在x轴下方显示为红色,col_C在x轴上方以绿色显示.这是matplotlib中的可能吗?如何更改以绘制所有三列?谢谢!解决方法:您可以通过向绘图的y参数提供列名列表来...

python – Pandas的性能与np.vectorize相对应,可以从现有列创建新列【代码】

我正在使用Pandas数据帧,并希望创建一个新列作为现有列的函数.我没有看到df.apply()和np.vectorize()之间的速度差异的很好的讨论,所以我想我会在这里问. Pandas apply()函数很慢.根据我的测量结果(在下面的一些实验中显示),使用np.vectorize()比使用DataFrame函数apply()快25倍(或更多),至少在我的2016 MacBook Pro上使用.这是预期的结果,为什么? 例如,假设我有以下具有N行的数据帧:N = 10 A_list = np.random.randint(1, 100, N...

python – Pandas将列表列表转换为dummies【代码】

我有一个数据框,其中一列是我的每个用户所属的组列表.就像是:index groups 0 ['a','b','c'] 1 ['c'] 2 ['b','c','e'] 3 ['a','c'] 4 ['b','e']我想要做的是创建一系列虚拟列,以确定每个用户所属的组,以便运行一些分析index a b c d e 0 1 1 1 0 0 1 0 0 1 0 0 2 0 1 1 0 1 3 1 0 1 0 0 4 0 1 0 0 0pd.get_dummies(df['groups']...

Python从pandas数据帧中删除停用词【代码】

我想从我的专栏“tweets”中删除停用词.如何迭代每一行和每个项目?pos_tweets = [('I love this car', 'positive'),('This view is amazing', 'positive'),('I feel great this morning', 'positive'),('I am so excited about the concert', 'positive'),('He is my best friend', 'positive')]test = pd.DataFrame(pos_tweets) test.columns = ["tweet","class"] test["tweet"] = test["tweet"].str.lower().str.split()from nl...

python – 具有多索引列的Pandas数据框 – 合并级别【代码】

我有一个数据框,分组,多指数列如下:import pandas as pd codes = ["one","two","three"]; colours = ["black", "white"]; textures = ["soft", "hard"]; N= 100 # length of the dataframe df = pd.DataFrame({ 'id' : range(1,N+1),'weeks_elapsed' : [random.choice(range(1,25)) for i in range(1,N+1)],'code' : [random.choice(codes) for i in range(1,N+1)],'colour': [random.choice(colours) for i in range(1,N+1)],'te...

python – 在Pandas DataFrame中将无效值替换为None【代码】

是否有任何方法可以在Python中的Pandas中用None替换值? 你可以使用df.replace(‘pre’,’post’)并且可以用另一个值替换一个值,但是如果要替换为None值,则无法执行此操作,如果尝试,则会得到奇怪的结果. 所以这是一个例子:df = DataFrame(['-',3,2,5,1,-5,-1,'-',9]) df.replace('-', 0)返回成功的结果. 但,df.replace('-', None)返回以下结果:0 0 - // this isn't replaced 1 3 2 2 3 5 4 1 5 -5 6 -1 7 -1 // thi...

python – 在pandas中设置切片值的正确方法【代码】

参见英文答案 > How to deal with SettingWithCopyWarning in Pandas? 13个我有一个pandas数据帧:数据.它有列[“名称”,“A”,“B”] 我想做(和工作)的是:d2 = data[data['name'] == 'fred'] #This gives me multiple rows d2['A'] = 0这会将fred行上的A列设置为0.我也做了:indexes = d2.index data['A'][indexes] = 0但是,两人都给了我同样的警告:/Users/brianp/work/cyan/venv/lib/pytho...

python – Pandas合并两个具有不同列的数据帧【代码】

我肯定在这里遗漏了一些简单的东西.尝试在大多数具有相同列名的pandas中合并两个数据帧,但是右边的数据帧有一些左边没有的列,反之亦然.>df_mayid quantity attr_1 attr_2 0 1 20 0 1 1 2 23 1 1 2 3 19 1 1 3 4 19 0 0>df_junid quantity attr_1 attr_3 0 5 8 1 0 1 6 13 0 1 2 7 20 ...

python – Pandas可以绘制日期的直方图吗?【代码】

我已经把我的系列强制转换为dtype = datetime64 [ns]的日期时间列(尽管只需要一天的分辨率……不知道如何更改).import pandas as pd df = pd.read_csv('somefile.csv') column = df['date'] column = pd.to_datetime(column, coerce=True)但绘图不起作用:ipdb> column.plot(kind='hist') *** TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('float64')我想绘制一个直方图,只显示按周,月或年的日...

python pandas dataframe,是值传递还是传递引用【代码】

如果我将数据帧传递给函数并在函数内修改它,它是按值传递还是按引用传递? 我运行以下代码a = pd.DataFrame({'a':[1,2], 'b':[3,4]}) def letgo(df):df = df.drop('b',axis=1) letgo(a)函数调用后a的值不会改变.这是否意味着它是传值? 我也试过以下xx = np.array([[1,2], [3,4]]) def letgo2(x):x[1,1] = 100 def letgo3(x):x = np.array([[3,3],[3,3]])事实证明,letgo2()确实改变了xx而letgo3()却没有改变.为什么会这样?解决方法...

python – 将pandas DataFrame转换为列表列表【代码】

我有一个像这样的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现在我想获得pandas中的行列表,如:[[0,3.61,380,3], [1,3.67,660,3], [1,3.19,640,4], [0,2.93,520,4]] 我该怎么做?解决方法:有一个内置的方法也是最快的方法,在.values np数组上调用tolist:df.values.tolist()[[0.0, 3.61, 380.0, 3.0],[1.0, 3.67, 660.0, 3.0],[1.0, 3.19, 64...

Python Pandas:将行转换为列标题【代码】

参见英文答案 > How to pivot a dataframe 1个我有以下数据帧:Year Country medal no of medals 1896 Afghanistan Gold 5 1896 Afghanistan Silver 4 1896 Afghanistan Bronze 3 1896 Algeria Gold 1 1896 Algeria Silver 2 1896 Algeria Bronze 3我想要这样.Year Country...

python – 使用pandas绘制相关矩阵【代码】

我有一个具有大量功能的数据集,因此分析相关矩阵变得非常困难.我想绘制一个相关矩阵,我们使用pandas库中的dataframe.corr()函数.是否有任何内置函数由pandas库提供以绘制此矩阵?解决方法:您可以使用matplotlib中的pyplot.matshow():import matplotlib.pyplot as pltplt.matshow(dataframe.corr()) plt.show()编辑: 在评论中提出了如何更改轴刻度标签的请求.这是一个豪华版本,绘制在更大的图形尺寸上,具有与数据框匹配的轴标签,以...

错误 - 相关标签