![](/img/trans.png)
[英]How to write a Python dataframe back to Excel File after using GroupBy?
[英]How to edit Excel file using DataFrame and save it back as Excel file?
我有这个Excel 文件。 我还将我的文件的屏幕截图放在下面。
我想用这 2 个标准编辑pitch-class
列上的数据:
' '
标记。0
值。[]
标记。因此,例如,从这段文字中:
['0', 'E3', 'F3', 'F#3 / Gb3', 'G3', 'G#3 / Ab3', 'A3', 'A#3 / Bb3', 'B3', 'C4', 'C#4 / Db4', 'D4']
我想让它看起来像这样:
[E3, F3, F#3 / Gb3, G3, G#3 / Ab3, A3, A#3 / Bb3, B3, C4, C#4 / Db4, D4]
当然,我可以一个一个地手动完成,但不幸的是因为我有大约 20 个类似的文件需要编辑,我无法手动完成,所以我想我可能需要 Python 的帮助。
My idea to do it on Python is to load the Excel file to a DataFrame, edit the data row by row (maybe using .remove()
and .join()
method), and put the edit result back to original Excel file, or可能会生成一个包含已编辑pitch-class
数据列的新列。
但是,我有点不知道如何编写代码。 到目前为止,我试图做的是:
pitch-class
列。import pandas as pd
file = '014_twinkle_twinkle 300 0.0001 dataframe.xlsx' # file attached above
df = pd.read_excel(file, index_col=None, usecols="C") # read only pitch-class column
# printing data
for row in df.iterrows():
print(df['pitch-class'].astype(str))
我的问题是如何编辑每行的pitch-class
数据并将编辑结果重新放回原始文件或新的 Excel 文件? 我无法访问df['pitch-class']
数据,因为我无法获取字符串值。 Python有什么办法可以实现吗?
通常,您不想遍历 pandas dataframe 中的每一行,这非常慢。 在 pandas 中,有很多方法(随着时间的推移,你可以通过练习来学习)在列/行/整个 dataframe 上应用函数。 在这个例子中:
将列转换为字符串类型,并将 ' 字符替换为空格
df = pd.read_excel("014_twinkle_twinkle 300 0.0001 dataframe.xlsx")
df["pitch-class"] = df["pitch-class"].astype(str).str.replace("'0', ", "")
df["pitch-class"] = df["pitch-class"].astype(str).str.replace("'", "")
df.to_excel("results.xlsx")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.