英文:
How to remove Freq: MS, Name: des, dtype: int64 from pandas series?
问题
以下是翻译好的部分:
我有一些像 `result_ses` 这样的pandas系列数据。我想将所有数据累积到一个字典中,并将数据保存在csv文件中。我正在使用Google Colab进行工作。但是我在删除数据中的一些不必要信息方面遇到了问题。我的代码如下:
asd = {}
for prod in unique_products[:4]:
asd[prod] = {} # 为每个产品创建一个空字典
asd[prod]['ses'] = result_ses
asd[prod]['des'] = result_des
print(asd)
输出如下:
{'2-28-437': {'ses': 2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64,
'des': 2021-05-01 14
2021-06-01 14
2021-07-01 13
Freq: MS, Name: des, dtype: int64},
'2-2-329': {'ses': 2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64,
'des': 2021-05-01 14
2021-06-01 14
2021-07-01 13
Freq: MS, Name: des, dtype: int64},
'24-30-42-7400': {'ses': 2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64,
'des': 2021-05-01 14
2021-06-01 14
2021-07-01 13
Freq: MS, Name: des, dtype: int64},
'2-53-1151': {'ses': 2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64,
'des': 2021-05-01 14
2021-06-01 14
2021-07-01 13
Freq: MS, Name: des, dtype: int64}}
其中`result_ses`和`result_des`都是pandas系列,`unique_products`是一个字符串列表。
# 如果我输入
result_ses.info()
# 我得到
<class 'pandas.core.series.Series'>
DatetimeIndex: 3 entries, 2021-05-01 to 2021-07-01
Freq: MS
Series name: ses
Non-Null Count Dtype
-------------- -----
3 non-null int64
dtypes: int64(1)
memory usage: 48.0 bytes
要查看`result_ses`的内容,我输入`print(result_ses)`并得到:
2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64 # 我不希望包含在csv文件中
我不希望字典`asd`包含这两个额外的信息,具体为**Freq: MS, Name: des, dtype: int64**,我只想保留其余部分,以便我可以在csv文件中获得所需的输出。使用以下代码,我尝试将数据保存在csv文件中,但它的格式不符合我的要求。
op_path = '/content/output/'
output_file_path = op_path + f'desired_output.csv'
ddf = pd.DataFrame.from_dict(asd, orient='index')
ddf.to_csv(output_file_path, index_label='Date')
我希望最终输出的csv文件如下所示。如何解决这个问题?
[![desired output image in csv][1]][1]
请注意,这个翻译保留了您提供的代码和问题的完整内容,只翻译了代码和问题的描述。
英文:
I have some pandas series like result_ses
. I wanted to accumulate all the data into a dictionary and save the data in a csv. I am using Google Colab to work. But I am facing trouble removing some of the unnecessary information from the data. My code is following:
asd = {}
for prod in unique_products[:4]:
asd[prod] = {} # empty dictionary for each product
asd[prod]['ses'] = result_ses
asd[prod]['des'] = result_des
print(asd)
The output is following:
{'2-28-437': {'ses': 2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64,
'des': 2021-05-01 14
2021-06-01 14
2021-07-01 13
Freq: MS, Name: des, dtype: int64},
'2-2-329': {'ses': 2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64,
'des': 2021-05-01 14
2021-06-01 14
2021-07-01 13
Freq: MS, Name: des, dtype: int64},
'24-30-42-7400': {'ses': 2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64,
'des': 2021-05-01 14
2021-06-01 14
2021-07-01 13
Freq: MS, Name: des, dtype: int64},
'2-53-1151': {'ses': 2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64,
'des': 2021-05-01 14
2021-06-01 14
2021-07-01 13
Freq: MS, Name: des, dtype: int64}}
Where both the result_ses
and result_des
are pandas series and unique_products
is a list of string.
# if I type
result_ses.info()
# I get
<class 'pandas.core.series.Series'>
DatetimeIndex: 3 entries, 2021-05-01 to 2021-07-01
Freq: MS
Series name: ses
Non-Null Count Dtype
-------------- -----
3 non-null int64
dtypes: int64(1)
memory usage: 48.0 bytes
To view the contents of the result_ses
I type print(result_ses)
and get:
2021-05-01 16
2021-06-01 16
2021-07-01 16
Freq: MS, Name: ses, dtype: int64 # I do not want this included in the csv
I do not want the dictionary asd
to include this two extra information specifically Freq: MS, Name: des, dtype: int64 and I want only the rest as it is so that I can get the desired output in the csv. Using the following code, I tried to save the data in the csv but it is not in the format I want.
op_path = '/content/output/'
output_file_path = op_path + f'desired_output.csv'
ddf = pd.DataFrame.from_dict(asd, orient='index')
ddf.to_csv(output_file_path, index_label='Date')
I am looking forward to getting the final output to be a csv like the following. How can I fix this problem?
答案1
得分: 1
以下是翻译好的代码部分:
你可以通过`DatetimeIndex`创建列,并为`ses`和`des`提供值:
asd = {}
for prod in unique_products[:4]:
asd[prod] = pd.DataFrame({'ses_date': result_ses.index,
'ses_val': result_ses.to_numpy(),
'des_date': result_des.index,
'des_val': result_des.to_numpy()})
df = pd.concat(asd)
要使用空格分隔不同的值:
asd = {}
for prod in unique_products[:4]:
ses = (result_ses.index.astype(str) + ' ' + result_ses.astype(str)).to_numpy()
des = (result_des.index.astype(str) + ' ' + result_des.astype(str)).to_numpy()
asd[prod] = pd.DataFrame({'ses': ses, 'des': des})
df = pd.concat(asd)
测试和解决方案运行良好:
result_ses = pd.Series([1, 2, 3], pd.date_range('2000-01-01', periods=3))
result_des = pd.Series([8, 9, 7], pd.date_range('2000-02-01', periods=3))
unique_products = ['2-28-437', '2-2-329', '24-30-42-7400', '2-53-1151']
asd = {}
for prod in unique_products[:4]:
ses = (result_ses.index.astype(str) + ' ' + result_ses.astype(str)).to_numpy()
des = (result_des.index.astype(str) + ' ' + result_des.astype(str)).to_numpy()
asd[prod] = pd.DataFrame({'ses': ses, 'des': des})
df = pd.concat(asd)
df.to_csv(f'modified_output.csv')
,,ses,des
2-28-437,0,2000-01-01 1,2000-02-01 8
2-28-437,1,2000-01-02 2,2000-02-02 9
2-28-437,2,2000-01-03 3,2000-02-03 7
2-2-329,0,2000-01-01 1,2000-02-01 8
2-2-329,1,2000-01-02 2,2000-02-02 9
2-2-329,2,2000-01-03 3,2000-02-03 7
24-30-42-7400,0,2000-01-01 1,2000-02-01 8
24-30-42-7400,1,2000-01-02 2,2000-02-02 9
24-30-42-7400,2,2000-01-03 3,2000-02-03 7
2-53-1151,0,2000-01-01 1,2000-02-01 8
2-53-1151,1,2000-01-02 2,2000-02-02 9
2-53-1151,2,2000-01-03 3,2000-02-03 7
如果需要在Excel中按独立行格式化:
result_ses = pd.Series([1, 2, 3], pd.date_range('2000-01-01', periods=3))
result_des = pd.Series([8, 9, 7], pd.date_range('2000-02-01', periods=3))
unique_products = ['2-28-437', '2-2-329', '24-30-42-7400', '2-53-1151']
asd = {}
for prod in unique_products[:4]:
ses = (result_ses.index.astype(str) + ' ' + result_ses.astype(str)).to_numpy()
des = (result_des.index.astype(str) + ' ' + result_des.astype(str)).to_numpy()
asd[prod] = pd.DataFrame({'ses': ses, 'des': des})
df = pd.concat(asd).groupby(level=0).agg('\n'.join).rename_axis('Date').reset_index()
with pd.ExcelWriter('modified_output.xlsx', engine='xlsxwriter') as writer:
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
cell_format = workbook.add_format({'text_wrap': True})
worksheet.set_column('A:Z', cell_format=cell_format)
请注意,以上翻译只包括代码部分,不包括注释和问题描述。
英文:
You can create columns by DatetimeIndex
and values for ses
and des
:
asd = {}
for prod in unique_products[:4]:
asd[prod] = pd.DataFrame({'ses_date':result_ses.index,
'ses_val':result_ses.to_numpy(),
'des_date':result_des.index,
'des_val':result_des.to_numpy()})
df = pd.concat(asd)
For separate values by space use:
asd = {}
for prod in unique_products[:4]:
ses = (result_ses.index.astype(str) +' ' + result_ses.astype(str)).to_numpy()
des = (result_des.index.astype(str) +' ' + result_des.astype(str)).to_numpy()
asd[prod] = pd.DataFrame({'ses':ses, 'des':des})
df = pd.concat(asd)
Testing and solution working nice:
result_ses = pd.Series([1,2,3], pd.date_range('2000-01-01', periods=3))
result_des = pd.Series([8,9,7], pd.date_range('2000-02-01', periods=3))
unique_products = ['2-28-437','2-2-329', '24-30-42-7400', '2-53-1151']
asd = {}
for prod in unique_products[:4]:
ses = (result_ses.index.astype(str) +' ' + result_ses.astype(str)).to_numpy()
des = (result_des.index.astype(str) +' ' + result_des.astype(str)).to_numpy()
asd[prod] = pd.DataFrame({'ses':ses, 'des':des})
df = pd.concat(asd)
df.to_csv(f'modified_output.csv')
,,ses,des
2-28-437,0,2000-01-01 1,2000-02-01 8
2-28-437,1,2000-01-02 2,2000-02-02 9
2-28-437,2,2000-01-03 3,2000-02-03 7
2-2-329,0,2000-01-01 1,2000-02-01 8
2-2-329,1,2000-01-02 2,2000-02-02 9
2-2-329,2,2000-01-03 3,2000-02-03 7
24-30-42-7400,0,2000-01-01 1,2000-02-01 8
24-30-42-7400,1,2000-01-02 2,2000-02-02 9
24-30-42-7400,2,2000-01-03 3,2000-02-03 7
2-53-1151,0,2000-01-01 1,2000-02-01 8
2-53-1151,1,2000-01-02 2,2000-02-02 9
2-53-1151,2,2000-01-03 3,2000-02-03 7
If need format with separate lines in Excel
:
result_ses = pd.Series([1,2,3], pd.date_range('2000-01-01', periods=3))
result_des = pd.Series([8,9,7], pd.date_range('2000-02-01', periods=3))
unique_products = ['2-28-437','2-2-329', '24-30-42-7400', '2-53-1151']
asd = {}
for prod in unique_products[:4]:
ses = (result_ses.index.astype(str) +' ' + result_ses.astype(str)).to_numpy()
des = (result_des.index.astype(str) +' ' + result_des.astype(str)).to_numpy()
asd[prod] = pd.DataFrame({'ses':ses, 'des':des})
df = pd.concat(asd).groupby(level=0).agg('\n'.join).rename_axis('Date').reset_index()
with pd.ExcelWriter('modified_output.xlsx', engine='xlsxwriter') as writer:
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
cell_format = workbook.add_format({'text_wrap': True})
worksheet.set_column('A:Z', cell_format=cell_format)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论