如何将所有行合并为1行?

huangapple go评论58阅读模式
英文:

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)

结果如下图所示:
如何将所有行合并为1行?

如何将所有行合并?

如何创建一个名为"Net Cash flow from investing cash flows"的行,并将值移到第一行。

如何将所有行合并为1行?

提前感谢!

英文:

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:
如何将所有行合并为1行?

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.
如何将所有行合并为1行?

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)

huangapple
  • 本文由 发表于 2023年5月29日 03:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定