我想使用apply函数:-以2列作为输入-基于函数输出两个新列。
这个add_multiply函数就是一个例子。
#function with 2 column inputs and 2 outputs
def add_multiply (a,b):
return (a+b, a*b )
#example dataframe
df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
#this doesn't work
df[['add', 'multiply']] = df.apply(lambda x: add_multiply(x['col1'], x['col2']), axis=1)
Run Code Online (Sandbox Code Playgroud)
理想结果:
col1 col2 add multiply
1 3 4 3
2 4 6 8
Run Code Online (Sandbox Code Playgroud) 我想找到存储我的 CODE 文件的 cwd。
有了木星实验室我会这样做:
import os
cwd= os.getcwd()
print (cwd)
OUT:
'C:...\\Jupiter_lab_notebooks\\CODE'
Run Code Online (Sandbox Code Playgroud)
但是,如果我将文件夹复制到我的 GoogleDrive,并在 GOOGLE COLAB 中运行笔记本,我会得到:
import os
cwd= os.getcwd()
print (cwd)
OUT:
/content
Run Code Online (Sandbox Code Playgroud)
无论我的笔记本存放在哪里。如何找到 .ipynb 文件夹存储的实际路径?
#编辑
我正在寻找的是 python 代码,它将返回 COLAB 笔记本的位置,无论它存储在驱动器中的哪个位置。这样我就可以从那里导航到子文件夹。
我有一个 GoogleDrive 文件夹的公共链接:https ://drive.google.com/drive/folders/19RUYQNOzMJEA-IJ3EKKUf0qGyyOepzGk ? usp = sharing
我想访问 colab 笔记本中的内容。 我希望打开笔记本的任何人都能够访问该文件夹,因此无需安装我自己的驱动器。在 Google Drive (Python) 中下载公共文件 等其他答案似乎建议对 ID 进行切片。我尝试按照说明https://towardsdatascience.com/3-ways-to-load-csv-files-into-colab-7c14fcbdcb92
link= 'https://drive.google.com/drive/folders/19RUYQNOzMJEA-IJ3EKKUf0qGyyOepzGk?usp=sharing'
fluff, id = link.split('=')
print (id)
Run Code Online (Sandbox Code Playgroud)
但是我的 id 只是“共享”
编辑代码仍然不起作用
然后运行代码:
from google.colab import auth
auth.authenticate_user() # must authenticate
'''list all ids of files directly under folder folder_id'''
def folder_list(folder_id):
from googleapiclient.discovery import build
gdrive = build('drive', 'v3').files()
res = gdrive.list(q="'%s' in parents" % folder_id).execute()
return [f['id'] for f in res['files']]
'''download all files from …Run Code Online (Sandbox Code Playgroud) 我有一个包含 2 个分类列(col1、col2)的数据框。
col1 col2
0 A DE
1 A B
2 B BA
3 A A
4 C C
Run Code Online (Sandbox Code Playgroud)
我想将唯一字符串值映射到整数,例如(A:0,B:1,BA:2,C:3,DE:4)
col1 col2 ideal1 ideal2
0 A DE 0 4
1 A B 0 1
2 B BA 1 2
3 A A 0 0
4 C C 3 3
Run Code Online (Sandbox Code Playgroud)
我尝试使用分解或类别,但我没有为两列获得相同的唯一值,如 ROW C 所示:
这是我的代码:
df = pd.DataFrame({'col1': ["A", "A", "B", "A" , "C"], 'col2': ["DE", "B", "BA", "A", "C"]})
#ideal map alphabetical: A:0, B:1, BA:2, C:3, DE:4 …Run Code Online (Sandbox Code Playgroud) 我想要:
来自如何通过按键杀死 while 循环? 我以使用键盘中断为例进行中断,这有效,但我想使用按钮。
键盘中断示例
weights = []
times = []
#open port
ser = serial.Serial('COM3', 9600)
try:
while True: # read infinite loop
#DO STUFF
line = ser.readline() # read a byte string
if line:
weight_ = float(line.decode()) # convert the byte string to a unicode string
time_ = time.time()
weights.append(weight_)
times.append(time_)
print (weight_)
#STOP it by keyboard interup and continue with program
except KeyboardInterrupt:
pass
#Continue with plotting
Run Code Online (Sandbox Code Playgroud)
不过我想用一个显示的按钮来做到这一点(更方便人们使用)。我尝试制作一个按钮(在 …
我有一个数据框(x,y,颜色),每当“颜色”更改值时,我都会尝试更改背景颜色。
到目前为止,我的结果具有背景颜色偏移:
我希望图像的颜色与颜色值的变化保持一致。
这是我运行的数据帧和代码:
y x color
0 0.691409 1.0 0
1 0.724816 2.0 0
2 0.732036 3.0 0
3 0.732959 4.0 0
4 0.734869 5.0 1
5 0.737061 6.0 2
6 0.717381 7.0 2
7 0.704016 8.0 2
8 0.693450 9.0 2
9 0.684942 10.0 2
10 0.674619 11.0 3
11 0.677481 12.0 3
12 0.680656 13.0 3
13 0.682392 14.0 3
14 0.682875 15.0 3
15 0.685440 16.0 4
16 0.678730 17.0 4
17 0.666658 18.0 …Run Code Online (Sandbox Code Playgroud)