我在一个 csv 文件中有数据,看起来像这样导入。
import csv
with open('Half-life.csv', 'r') as f:
data = list(csv.reader(f))
Run Code Online (Sandbox Code Playgroud)
数据将作为这个输出到它打印出诸如此类的行的地方data[0] = ['10', '2', '2']。
我想要的是将数据作为列而不是行检索,在这种情况下,有 3 列。
如何计算具有少量数组的列表中某个整数的出现?例如,我想查找值2的出现次数.
import numpy as np
a = [np.array([2, 2, 1, 2]), np.array([1, 3])]
Run Code Online (Sandbox Code Playgroud)
预期产量:
[3, 0]
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
我有一个Python/Pandas数据帧(df1),由ID,Chr和位置组成.和由相同类型的数据(ID,Chr,位置),df2组成的数据帧.
我想获得第三个数据帧(df3),该数据帧仅保留基于df1和df2之间的Chr列的df1行,以及df2的pos-start和pos-end内的位置.另外,它需要添加匹配所源自的ID或行df2.
我发现这很困难,有没有人有想法?
请看下面的例子:
df1:
ID1 Chr pos
a 12 500
b 12 250
c 12 300
d 16 2000
e 16 1050
f 16 1075
d 16 1150
g 17 8000
h 17 550
i 17 500
Run Code Online (Sandbox Code Playgroud)
df2:
ID2 Chr pos-start pos-end
x 12 200 400
y 16 1000 1100
z 16 1070 1200
Run Code Online (Sandbox Code Playgroud)
得到的df3:
ID2 ID1 Chr Pos
x b 12 250
x c 12 300
y e …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的数据框
EndDate
2007-10-31 0
2007-11-30 -0.03384464
2007-12-31 -0.0336299
2008-01-31 -0.009448923
2008-02-29 0
2008-03-31 -0.05744962
2008-04-30 -0.0386942
2008-05-31 0
2008-06-30 -0.03624518
2008-07-31 -0.005286455
2008-08-31 0
2008-09-30 -0.1619864
2008-10-31 -0.2862122
2008-11-30 -0.2942793
2008-12-31 -0.2913253
Run Code Online (Sandbox Code Playgroud)
现在我想在每次出现 0 后分割数据帧。因此新的数据帧应该如下所示:
Dataframe 1:
2007-11-30 -0.03384464
2007-12-31 -0.0336299
2008-01-31 -0.009448923
2008-02-29 0
Dataframe 2:
2008-03-31 -0.05744962
2008-04-30 -0.0386942
2008-05-31 0
Dataframe 3:
2008-06-30 -0.03624518
2008-07-31 -0.005286455
2008-08-31 0
Dataframe 4:
2008-09-30 -0.1619864
2008-10-31 -0.2862122
2008-11-30 -0.2942793
2008-12-31 -0.2913253
Run Code Online (Sandbox Code Playgroud)
我不知道如何做到这一点。我可以迭代每一行寻找 0 但我认为应该有更好的方法。
在我的数据框中,某些行中有NaN值.我想删除这些行.我用dataframe.dropna(how ='any')解决了这个问题.结果如下:
date time open hign low close volume turnover
2 2015-09-01 931 48.60 48.60 48.00 48.00 449700 21741726
3 2015-09-01 932 47.91 48.33 47.91 48.25 158500 7614508
Run Code Online (Sandbox Code Playgroud)
我想重新索引我的数据帧的行,所以我运行:
length = dataframe.dropna(how='any').shape[0]
dataframe1 = dataframe.index(range(length))
Run Code Online (Sandbox Code Playgroud)
但是dataframe1仍然保留旧的索引值,例如:
date time open hign low close volume turnover
0 NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN NaN NaN NaN
2 2015-09-01 931 48.60 48.60 48.00 48.00 449700 21741726
3 2015-09-01 932 47.91 48.33 47.91 48.25 158500 7614508 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用我编写的函数将 pandas DataFrame 中的信息插入到数据库表中:
\n\ndef insert(table_name="", name="", genere="", year=1, impd_rating=float(1)):\n conn = psycopg2.connect("dbname=\'database1\' user=\'postgres\' password=\'postgres333\' host=\'localhost\' port=5433 ")\n cur = conn.cursor()\n cur.execute("INSERT INTO %s VALUES %s,%s,%s,%s" % (table_name, name, genere, year, impd_rating))\n conn.commit()\n conn.close()\nRun Code Online (Sandbox Code Playgroud)\n\n当我尝试像这样使用这个函数时:
\n\nb=0\nfor row in DF[\'id\']:\n insert(impd_rating=float(DF[\'idbm_rating\'][b]), \n year=int(DF[\'year\'][b]), \n name=str(DF[\'name\'][b]), \n genere=str(DF[\'genere\'][b]),\n table_name=\'test_movies\')\n b = b+1\nRun Code Online (Sandbox Code Playgroud)\n\n我收到以下语法错误:
\n\nSyntaxError:\xc2\xa0invalid\xc2\xa0syntax\nPS\xc2\xa0D:\\tito\\scripts\\database\xc2\xa0training>\xc2\xa0python\xc2\xa0.\\postgres_script.py\nTraceback\xc2\xa0(most\xc2\xa0recent\xc2\xa0call\xc2\xa0last):\nFile\xc2\xa0".\\postgres_script.py",\xc2\xa0line\xc2\xa056,\xc2\xa0in\xc2\xa0<module>insert\xc2\xa0(impd_rating=float(DF[\'idbm_rating\'][b]),year=int(DF[\'year\'][b]),name=str(DF[\'name\'][b]),genere=str(DF[\'genere\'][b]),table_name=\'test_movies\')\nFile\xc2\xa0".\\postgres_script.py",\xc2\xa0line\xc2\xa015,\xc2\xa0in\xc2\xa0insert\ncur.execute("INSERT\xc2\xa0INTO\xc2\xa0%s\xc2\xa0VALUES\xc2\xa0%s,%s,%s,%s"\xc2\xa0\xc2\xa0%\xc2\xa0(table_name\xc2\xa0,name\xc2\xa0,genere\xc2\xa0,\xc2\xa0year,impd_rating))\npsycopg2.ProgrammingError:\xc2\xa0syntax\xc2\xa0error\xc2\xa0at\xc2\xa0or\xc2\xa0near\xc2\xa0"Avatar"\nLINE\xc2\xa01:\xc2\xa0INSERT\xc2\xa0INTO\xc2\xa0test_movies\xc2\xa0VALUES\xc2\xa0Avatar,action,2009,7.9\nRun Code Online (Sandbox Code Playgroud)\n\n我还尝试将 str 替换方法从 更改%s为.format()\n但我遇到了同样的错误。
[{50: 2, 75: 3, 99: 4}, {50: 5, 75: 6, 99: 7}, {50: 8, 75: 9, 99:10}]
Run Code Online (Sandbox Code Playgroud)
以下是 Python 中的字典数组。我想在 Python 中将数组转换为以下三个数组:
[2, 5, 8] # corresponding to 50.
[3, 6, 9] # corresponding to 75.
[4, 7, 9] # corresponding to 99.
Run Code Online (Sandbox Code Playgroud) 我正在努力准备面试,并想为句子中每个字母的计数制作一本字典。
我很惊讶地发现我可能存在范围界定问题,或者看起来像是问题。
对于下面的具体方法,我不确定为什么它不起作用,直到我发现 elem 在 while 循环中被设置为空。但由于它嵌套在引用 elem 的 for 循环中,我不确定为什么会发生这种情况。
s = 'letters and stuff'
def d_count(s):
dic = {}
i = 0
s1 = set(s)
s2 = list(s)
for elem in s1:
dic[elem] = 0
j = 0
while (i < len(s2)):
#elem disappears here
if s2[i] == elem:
j = j+1
dic[elem] = j
i = i + 1
return dic
print(d_count(s))
Run Code Online (Sandbox Code Playgroud)
结果
{' ': 2, 's': 0, 'n': 0, 'u': 0, 'l': 0, 'f': 0, 'd': …Run Code Online (Sandbox Code Playgroud) 这是一个Python脚本的示例:
n, dollar, euro = map(input().split())
if n == 1:
print(dolar / euro) # Note the typo, 'dolar' vs. 'dollar'
else:
print(euro / dollar)
Run Code Online (Sandbox Code Playgroud)
我犯了一个错误("dolar"与一个'l').error(NameError)除非您输入,否则口译员不会通知甚至是警告n = 1.
如何在运行时通知它?
我有两个数据框:
df1
A1 B1
1 a
2 s
3 d
Run Code Online (Sandbox Code Playgroud)
和
df2
A1 B1
1 a
2 x
3 d
Run Code Online (Sandbox Code Playgroud)
我想比较B1列上的df1和df2.列A1可用于连接.我想知道:
我尝试使用合并和连接,但这不是我想要的.
python ×10
pandas ×4
dataframe ×2
numpy ×2
arrays ×1
csv ×1
dictionary ×1
matplotlib ×1
merge ×1
nameerror ×1
postgresql ×1
psycopg2 ×1
python-2.7 ×1