Python 使用每个值2个键将csv读入字典,并将第一个非零数字指定给字典值
我想将csv读入字典,其键为wellID和months 1-12,值为数据中的第一个非零值。我的输出仅捕获数据集中的第二个月(12):Python 使用每个值2个键将csv读入字典,并将第一个非零数字指定给字典值,python,pandas,csv,dictionary,Python,Pandas,Csv,Dictionary,我想将csv读入字典,其键为wellID和months 1-12,值为数据中的第一个非零值。我的输出仅捕获数据集中的第二个月(12): 'Month': {}, 'MUN-1': {12.0: -292.1183432}, 'MUN-42': {12.0: -3566.994083}, 'MUN-57': {}, 我的数据如下所示: df2=pd.read\u csv('wells.csv')) 行=df2.apply(lambda x:x.tolist(),轴=1) lst=[]
'Month': {},
'MUN-1': {12.0: -292.1183432},
'MUN-42': {12.0: -3566.994083},
'MUN-57': {},
我的数据如下所示:
df2=pd.read\u csv('wells.csv'))
行=df2.apply(lambda x:x.tolist(),轴=1)
lst=[]
对于行中的i:
一、附加(一)
N=1
wellID={words[0]:对于lst}中的单词,单词[1:]
名称=[]
对于wellID.keys()中的i:
名称.附加(i)
namedict=dict()
对于名称中的名称:
namedict[name]=dict()
timeseries=dict()
dict(itertools.islice(namedict.items(),N))
idx=0
对于wellID中的月份[“月”]:
对于wellID.keys()中的井:
timeseries[well]=dict()
如果wellID[well][idx]order=list(map(int,list(df2)[1:]))
答案={}
在wellID.keys()中的月份:
索引=下一个((i表示枚举中的i,x)(如果x为wellID[月]),无)
i=顺序[索引]
值=井号[月份][指数]
答案[月份]={索引:值}
尝试用这个替换最后一个for循环 请发布您的数据,而不是图片。以此为指导
df2 = pd.read_csv('wells.csv')
rows = df2.apply(lambda x: x.tolist(), axis=1)
lst = []
for i in rows:
lst.append(i)
N=1
wellID = {words[0]:words[1:] for words in lst}
names = []
for i in wellID.keys():
names.append(i)
namesdict = dict()
for name in names:
namesdict[name]=dict()
timeseries=dict()
dict(itertools.islice(namesdict.items(),N))
idx=0
for month in wellID["Month"]:
for well in wellID.keys():
timeseries[well]=dict()
if wellID[well][idx]<0:
currentMonth=wellID["Month"][idx]
timeseries[well][currentMonth]=dict()
if currentMonth not in timeseries.keys():
timeseries[well][currentMonth]=wellID[well][idx]
idx=+1