我有很多不同的表(以及excel表中的其他非结构化数据).我需要在Excel表'数据'的'Sheet2'中创建一个超出范围'A3:D20'的数据帧.
我遇到的所有示例都向下钻取到工作表级别,但不是如何从精确范围中选择它.
import openpyxl
import pandas as pd
wb = openpyxl.load_workbook('data.xlsx')
sheet = wb.get_sheet_by_name('Sheet2')
range = ['A3':'D20'] #<-- how to specify this?
spots = pd.DataFrame(sheet.range) #what should be the exact syntax for this?
print (spots)
Run Code Online (Sandbox Code Playgroud)
一旦我得到这个,我计划在A列中查找数据并在B列中找到它的相应值.
编辑1:我意识到openpyxl需要太长时间,所以改为将其pandas.read_excel('data.xlsx','Sheet2')改为,至少在那个阶段它要快得多.
编辑2:目前,我已将我的数据放在一张表中,并且:
index_col在我最左边的列上wb.loc[]我遇到了这个讨论(从一年前开始):https://github.com/bokeh/bokeh/issues/2392
我也看到白屏没有任何错误..然后我试图采取2列的一小部分,并尝试以下:
由于pandas只是在那里获得了一堆空数据,我尝试了dropna ..这导致根本没有数据.所以我只是指定应该进入df的df = df.head(n=19)行(因此行)
import pandas as pd
from bokeh.plotting import figure, output_file, show
df = pd.read_excel(path,sheetname,parse_cols="A:B")
df = df.head(n=19)
print(df)
rtngs = ['iAAA','iAA+','iAA','iAA-','iA+','iA','iA-','iBBB+','iBBB','iBBB-','iBB+','iBB','iBB-','iB+','iB','iB-','NR','iCCC+']
x= df['Score']
output_file("line.html")
p = figure(plot_width=400, plot_height=400, x_range=(0,100),y_range=rtngs)
# add a circle renderer with a size, color, and alpha
p.circle(df['Score'], df['Rating'], size=20, color="navy", alpha=0.5)
# show the results
#output_notebook()
show(p)
Run Code Online (Sandbox Code Playgroud)
DF:
Rating Score
0 iAAA 64.0
1 iAA+ 33.0
2 iAA 7.0
3 iAA- 28.0
4 iA+ 36.0
5 …Run Code Online (Sandbox Code Playgroud) UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 8: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
我在一个简单的SQL查询中看到了错误:
df = pd.read_sql(query,connection)
Run Code Online (Sandbox Code Playgroud)
我通常在sql开发人员上尝试了查询,它运行得很好..这里真的难以理解如何在read_sql调用中指定编码
我使用的是Python 3.4和pandas版本0.14.1
非常感谢!
有没有快速实现以下输出的方法?
输入:
Code Items
123 eq-hk
456 ca-eu; tp-lbe
789 ca-us
321 go-ch
654 ca-au; go-au
987 go-jp
147 co-ml; go-ml
258 ca-us
369 ca-us; ca-my
741 ca-us
852 ca-eu
963 ca-ml; co-ml; go-ml
Run Code Online (Sandbox Code Playgroud)
输出:
Code eq ca go co tp
123 hk
456 eu lbe
789 us
321 ch
654 au au
987 jp
147 ml ml
258 us
369 us,my
741 us
852 eu
963 ml ml ml
Run Code Online (Sandbox Code Playgroud)
我再次遇到循环和一个非常难看的代码,使其工作.如果有一种优雅的方式来实现这一点?
谢谢!
我有以下内容df1:
Date Tickers Qty
01-01-2018 ABC 25
02-01-2018 BCD 25
02-01-2018 XYZ 31
05-01-2018 XYZ 25
Run Code Online (Sandbox Code Playgroud)
和另一个df2如下
Date ABC BCD XYZ
01-01-2018 123 5 78
02-01-2018 125 7 79
03-01-2018 127 6 81
04-01-2018 126 7 82
05-01-2018 124 6 83
Run Code Online (Sandbox Code Playgroud)
我想要一个结果列,df1其中是正确的列和行的乘积df2- 在给定日期获得正确的股票价格,并让其他日期在df1
Date df1['Product']
01-01-2018 3075
02-01-2018 175
02-01-2018 2449
03-01-2018 nan
04-01-2018 nan
05-01-2018 2075
Run Code Online (Sandbox Code Playgroud)
这看起来像标准的python操作,但我无法在没有编写循环的情况下实现这一点 - 这需要很长时间才能执行:
我合并了上面的两个表Date,然后运行下面的循环
for i in range(len(df1)):
try:
df1['Product'][i] = df1[df1['Ticker'][i]][i] …Run Code Online (Sandbox Code Playgroud) 我有2个表,它们具有相同的列,我想在键匹配的地方添加数字,如果不匹配,则直接在输出df中添加它。他们都为t1和t2创建了2个单独的列,但是它们具有相同的键,所以应该在一起,我知道这将是非常基本的事情。谢谢vm!
df1:
t1 a b
0 USD 2,877 -2,418
1 CNH 600 -593
2 AUD 756 -106
3 JPY 113 -173
4 XAG 8 0
df2:
t2 a b
0 CNH 64 -44
1 USD 756 -774
2 JPY 1,127 -2,574
3 TWO 56 -58
4 TWD 38 -231
Output:
t a b
USD 3,634 -3,192
CNH 664 -637
AUD 756 -106
JPY 1,240 -2,747
XAG 8 0
TWO 56 -58
TWD 38 -231
Run Code Online (Sandbox Code Playgroud) 我有一个df,在最左边的列中有一些代码,在其他列中有一个前向轮廓(下面的df1)
DF1:
code tp1 tp2 tp3 tp4 tp5 tp6 \
0 1111 0.000000 0.000000 0.018714 0.127218 0.070055 0.084065
1 222 0.000000 0.000000 0.000418 0.000000 0.017540 0.003015
2 333 1.146815 1.305678 0.384918 0.688284 0.000000 0.000000
3 444 0.000000 0.000000 1.838797 0.000000 0.000000 0.000000
4 555 27.190002 27.134837 24.137560 17.739465 11.990806 8.631395
5 666 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
tp7 tp8 tp9 tp10
0 0.019707 0.000000 0.000000 0.000000
1 6.594860 10.535905 15.697232 21.035824
2 0.000000 0.000000 0.000000 0.000000
3 0.000000 0.000000 …Run Code Online (Sandbox Code Playgroud)