我有一个清单:
data = [
{'A': [2.0, 3.0, 4.0, 5.0, 6.0], 'B':[27.0, 28.0, 29.0, 30.0], 'C': ['lic1'],
'D': ['soy1'], 'E': ['foo1']},
{'A': [7.0, 11.0, 90.0, 43.0, 87.0], 'B':[27.0, 28.0, 29.0, 30.0], 'C': ['lic1'],
'D': ['soy1'], 'E': ['foo1']},
# ... etc
Run Code Online (Sandbox Code Playgroud)
]
“ A”上的数据是熊猫系列。我想计算“ A”中数据的平均偏差和标准偏差(A有多个记录),例如:(平均值=(2.0 + 3.0 + 4.0 + 5.0 + 6.0 + 7.0 + 11.0 + 90.0 + 43.0 + 87.0)/ len(A)= 25.8)
我想读取文本文件并检查字符串
with open(my_file,'r') as f:
for line in f:
if 'text1' in line:
f.next()
if 'text2' in line:
# do some processing
Run Code Online (Sandbox Code Playgroud)
我想首先在行的开头找到文本“text1”,然后如果找到,我想检查下一行是否有“text2”,如果找到,那么我将做一些其他处理。似乎 f.next() 没有移动到下一行。
我正在将.csv文件读入pandas数据帧..csv文件包含多个列.列'A'包含字符串'20 -989-98766'.是否可以在加载文件时从字符串中读取最后5个字符'98766'?
df = pd.read_csv("test_data2.csv", column={'A':read the last 5 characters})
Run Code Online (Sandbox Code Playgroud)
输出:
A
98766
95476
.....
Run Code Online (Sandbox Code Playgroud) 我有一个大的(垂直)熊猫数据框,我想将其显示为带有滚动条的漂亮表格。我可以显示所有行的表,但是无法显示滚动条。
def data(x):
strData = strData[['Data1','Data2','Data3']]
display(strData)
Run Code Online (Sandbox Code Playgroud)
输出:无垂直滚动条
我正在将 .csv 文件读入 Pandas 数据帧。我将文件读为:
df = pd.read_csv(filepath, sep='\t')
Run Code Online (Sandbox Code Playgroud)
文本标题看起来像(\s 空格 \t 制表符)
Date\s\s\tPlace\s\s\s\s\s\tLow\s\s\s\s\tHigh\s\s\s\s\t
我正在使用 '\t' sep 将文件读入数据帧。但问题是,如果我尝试使用 df['Date'] 访问其中一列,我会收到 KeyError: 'Date'。我认为熊猫在阅读列时不会删除空格和制表符?如何从列标题中删除空格和制表符?
我有一个像这样的熊猫数据框:
df = pd.DataFrame({'A':[1,1,1,2,2,2,3,3,3],
'B':[3,2,20,1,6,2,3,22,1]})
Run Code Online (Sandbox Code Playgroud)
我想找到列'B'中的'max'值然后从'B'列的所有值中减去这个最大值,并用新结果创建一个新列'C'.底部df的最大值为22.
A B C
2 1 3 -19
1 1 2 -20
0 1 20 -2
3 2 1 -21
5 2 6 -16
4 2 2 -20
8 3 3 -19
7 3 22 0
6 3 1 -21
Run Code Online (Sandbox Code Playgroud) 我想用丢失的文件名引发异常:
f1 = Path(DIR_PATH, "one.txt").resolve()
f2 = Path(DIR_PATH, "two.txt").resolve()
# 1 Is there a way of doing this?
if not f1.exists() or not f2.exists():
raise Exception(f"The file {?} does not exist")
# 2 instead of this
if not f1.exists():
raise Exception(f"The file {f1} does not exist")
elif not f2.exists():
raise Exception(f"The file {f2} does not exist")
Run Code Online (Sandbox Code Playgroud)
有没有办法从上面的代码中做#1而不是#2?
我有一个字符串数组数组,['one','two',three']并希望将其转换为键值对,使其看起来像(第一个元素是键,最后一个元素是值):
{
one:'three'
}
Run Code Online (Sandbox Code Playgroud)
这是我得到了多远:
function t(array) {
var key = array[0];
return {key:array[array.length-1]}
}
Run Code Online (Sandbox Code Playgroud)
输出:
{ key: 'three' }
Run Code Online (Sandbox Code Playgroud)
值正确但键未正确显示.