英文:
How to merge/combine all rows to 1?
问题
这是我的脚本:
import pandas as pd
wbSheets = pd.ExcelFile("Jaaroverzichten Cashflow.xlsx").sheet_names
frames = []
for st in wbSheets:
df = pd.read_excel("Jaaroverzichten cashflow.xlsx", st)
frames.append(df.loc[df["Onderdelen"].str.lower().str.contains('cash flows') & df["Onderdelen"].str.lower().str.contains('investing activities')])
res = pd.concat(frames)
res2 = res.columns[1:]
res.to_csv('Test.csv', index=False)
结果如下图所示:
如何将所有行合并?
如何创建一个名为"Net Cash flow from investing cash flows"的行,并将值移到第一行。
提前感谢!
英文:
This is my script:
import pandas as pd
wbSheets = pd.ExcelFile("Jaaroverzichten Cashflow.xlsx").sheet_names
frames = []
for st in wbSheets:
df = pd.read_excel("Jaaroverzichten cashflow.xlsx",st)
frames.append(df.loc[df["Onderdelen"].str.lower().str.contains('cash flows') & df["Onderdelen"].str.lower().str.contains('investing activities')])
res = pd.concat(frames)
res2 = res.columns[1:]
res.to_csv('Test.csv', index=False)
The result is in image below:
How to combine all the rows?
How to create one row "Net Cash flow from investing cash flows".
And move the values to the first row.
thanks in advance!
答案1
得分: 0
你可以使用以下代码:
dfs = pd.concat(
pd.read_excel("Jaaroverzichten Cashflow.xlsx", sheet_name=None), ignore_index=True
)
m = dfs["Onderdelen"].str.contains("cash flows|investing activities", case=False)
dfs.loc[m, "Onderdelen"] = "Net Cash flow from investing cash flows"
res = dfs.loc[m].groupby("Onderdelen", as_index=False).first()
res.to_csv("Test.csv", index=False)
英文:
You can use :
dfs = pd.concat(
pd.read_excel("Jaaroverzichten Cashflow.xlsx", sheet_name=None), ignore_index=True
)
m = dfs["Onderdelen"].str.contains("cash flows|investing activities", case=False)
dfs.loc[m, "Onderdelen"] = "Net Cash flow from investing cash flows"
res = dfs.loc[m].groupby("Onderdelen", as_index=False).first()
res.to_csv("Test.csv", index=False)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论