我有一个pandas数据帧(通过导入csv文件创建).我想用NaN替换空白值.一些这些空白值的是空的,一些包含一个(变量数)的空间'',' ',' '等.
使用这个帖子的建议我有
df.replace(r'\s+', np.nan, regex=True, inplace = True)
Run Code Online (Sandbox Code Playgroud)
它确实替换了只包含空格的所有字符串,但也替换了其中包含空格的每个字符串,这不是我想要的.
我怎么只替换字符串只是空间和空字符串?
我有一个.csv文件,其中顶行和第一列包含字符串,其余数据为浮点数。我想将其读入数据帧,其中第一行和第一列分别作为列名称和索引,所有浮动值作为float64.
如果我使用df = pd.read_csv(filename,index_col=0)所有数值都保留为字符串。
如果我使用df = pd.read_csv(filename, index_col=0, dtype=np.float64)我会得到一个异常:ValueError: could not convert string to float因为它尝试将第一列解析为float.
有大量列,并且我没有列名称,因此我不想将每一列标识为float; 我想解析除第一列之外的每一列。
我在并行计算集群的不同处理器上运行Python 3.6脚本作为多个单独的进程.最多35个进程同时运行没有问题,但第36行(以及更多)在第二行崩溃并出现分段错误import pandas as pd.有趣的是,第一行import os不会引起问题.完整的错误消息是:
OpenBLAS blas_thread_init: pthread_create: Resource temporarily unavailable
OpenBLAS blas_thread_init: RLIMIT_NPROC 1024 current, 2067021 max
OpenBLAS blas_thread_init: pthread_create: Resource temporarily unavailable
OpenBLAS blas_thread_init: RLIMIT_NPROC 1024 current, 2067021 max
OpenBLAS blas_thread_init: pthread_create: Resource temporarily unavailable
OpenBLAS blas_thread_init: RLIMIT_NPROC 1024 current, 2067021 max
OpenBLAS blas_thread_init: pthread_create: Resource temporarily unavailable
OpenBLAS blas_thread_init: RLIMIT_NPROC 1024 current, 2067021 max
OpenBLAS blas_thread_init: pthread_create: Resource temporarily unavailable
OpenBLAS blas_thread_init: RLIMIT_NPROC 1024 current, 2067021 max
OpenBLAS blas_thread_init: pthread_create: Resource temporarily …Run Code Online (Sandbox Code Playgroud) 我有一个字典dictData,它是从csv文件的3列(0,3和4)创建的,其中每个键都是一个日期时间对象,每个值都是一个列表,包含两个数字(让我们称它们为a和b,所以列表是[a,b])存储为字符串:
import csv
import datetime as dt
with open(fileInput,'r') as inFile:
csv_in = csv.reader(inFile)
dictData = {(dt.datetime.strptime(rows[0],'%d/%m/%Y %H:%M')):[rows[3],rows[4]] for rows in csv_in}
Run Code Online (Sandbox Code Playgroud)
我想做两件事:首先,我想对整个字典中的每个值求和(即对所有值进行求和,然后对所有b值求和).如果它是单值的字典,我会做这样的事情:
total = sum((float(x) for x in dictData.values()))
Run Code Online (Sandbox Code Playgroud)
如何更改此项以便.values标识列表中的第一个(或第二个)项目?(即a或b值)
我想计算列表中第一项的所有零值.
我正在同一轴上绘制一系列箱线图,并希望添加图例来识别它们。非常简化,我的脚本如下所示:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df={}
bp={}
positions = [1,2,3,4]
df[0]= pd.DataFrame (np.random.rand(4,4),columns =['A','B','C','D'])
df[1]= pd.DataFrame (np.random.rand(4,4),columns =['A','B','C','D'])
colour=['red','blue']
fig, ax = plt.subplots()
for i in [0,1]:
bp[i] = df[i].plot.box(ax=ax,
positions = positions,
color={'whiskers': colour[i],
'caps': colour[i],
'medians': colour[i],
'boxes': colour[i]}
)
plt.legend([bp[i] for i in [0,1]], ['first plot', 'second plot'])
fig.show()
Run Code Online (Sandbox Code Playgroud)
情节很好,但图例没有绘制,我收到此警告
UserWarning: Legend does not support <matplotlib.axes._subplots.AxesSubplot object at 0x000000000A7830F0> instances.
A proxy artist may be used instead.
Run Code Online (Sandbox Code Playgroud)
(我之前在向散点图添加图例时收到过此警告,但图例仍然被绘制,所以我可以忽略它。)
我正在使用一个共享的 github 存储库来协作一个项目。因为我是个白痴,所以我提交并推送了一个包含我不想共享的密码的脚本文件(是的,我可以更改密码,但无论如何我想删除它!)。
有没有办法从github的历史记录中恢复提交,在本地删除密码,然后重新提交并推送更新的文件?我不想完全删除文件,也不想丢失 github 上的提交历史记录。
(这个问题 如何从 git 存储库中完全删除文件? 显示了如何删除敏感文件,但没有显示如何从文件中编辑敏感数据,所以这不是重复的)
我有一个熊猫数据框DatetimeIndex,例如
In: ts = pd.date_range('2013-01-01 00:00', periods=17520, freq='30min')
In: values = list(range(1, 17520))
In: df = pd.DataFrame(values, index=ts)
Run Code Online (Sandbox Code Playgroud)
我想重塑数据框,以便将日期作为索引,将小时[0,0.5,1.0,1.5,.....]作为列。
我有这个:
df.pivot(index=df.index.date, columns=df.index.time, values='values')
Run Code Online (Sandbox Code Playgroud)
但它给了我一个key error日期列表not in index
我正在创建一个空数据框,然后我想一次将数据添加到一行。我想索引第一列,'customer_ID'
我有这个:
In[1]: df = pd.DataFrame(columns = ['customer_ID','a','b','c'],index=['customer_ID'])
In[2]: df
Out[3]:
customer_ID a b c
customer_ID NaN NaN NaN NaN
Run Code Online (Sandbox Code Playgroud)
所以已经有一排NaN我不想要的了。我可以将索引指向第一列而不添加一行数据吗?
我是新手,matplotlib并试图显示我通过函数 read_API() 从 api 下载的三个变量的最后一小时数据的实时图。数据位于带有 DateTimeIndex 的 Pandas 数据框中。例如:
In: dframe.head()
Out:
A B C
timestamp
2017-05-11 16:21:55 0.724931 0.361333 0.517720
2017-05-11 16:22:25 0.725386 0.360833 0.518632
2017-05-11 16:22:55 0.725057 0.361333 0.521157
2017-05-11 16:23:25 0.724402 0.362133 0.520002
Run Code Online (Sandbox Code Playgroud)
简化的代码是:
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
while True:
dframe = read_API()
dframe['timestamp'] = dframe['timestamp'] + pd.DateOffset(hours=timezone)
dframe = dframe.set_index('timestamp')
end = dframe.index.max()
start= end.to_datetime() - dt.timedelta(hours=1)
dframe = dframe.loc[start:end]
plt.ion()
fig, ax = plt.subplots()
plt.pause(0.0001) …Run Code Online (Sandbox Code Playgroud) 我正在绘制一个带有 6 组轴的图形,每个轴都有来自 2 个 Pandas 数据帧之一的一系列 3 行(每列 1 行)。我一直在使用matplotlib .plot:
import pandas as pd
import matplotlib.pyplot as plt
idx = pd.DatetimeIndex(start = '2013-01-01 00:00', periods =24,freq = 'H')
df1 = pd.DataFrame(index = idx, columns = ['line1','line2','line3'])
df1['line1']= df1.index.hour
df1['line2'] = 24 - df1['line1']
df1['line3'] = df1['line1'].mean()
df2 = df1*2
df3= df1/2
df4= df2+df3
fig, ax = plt.subplots(2,2,squeeze=False,figsize = (10,10))
ax[0,0].plot(df1.index, df1, marker='', linewidth=1, alpha=1)
ax[0,1].plot(df2.index, df2, marker='', linewidth=1, alpha=1)
ax[1,0].plot(df3.index, df3, marker='', linewidth=1, alpha=1)
ax[1,1].plot(df4.index, df4, …Run Code Online (Sandbox Code Playgroud)