我想合并 4 行以形成 1 行,其中包含 pandas 数据帧中的 4 个子行



这是我的数据帧

我试过这个,但没有用:

df1['quarter'].str.contains('/^[-+](20)$/', re.IGNORECASE).groupby(df1['quarter'])

提前致谢

您好,欢迎来到论坛!如果我理解正确了你的问题,你想每年组队吗?

当然,您可以简单地按每年进行分组,因为您已经有了该列。

假设您没有年份列,您可以简单地按整个字符串分组,除了四分之一列的最后 2 个字符。像这样(我为答案创建了一个玩具数据集(:

import pandas as pd
d = {'quarter' : pd.Series(['1947q1', '1947q2', '1947q3', '1947q4','1948q1']), 
'some_value' : pd.Series([1,3,2,4,5])}
df = pd.DataFrame(d)
df

这是我们的玩具数据框:

quarter     some_value
0   1947q1  1
1   1947q2  3
2   1947q3  2
3   1947q4  4
4   1948q1  5

现在我们简单地按年份分组,但我们减去最后 2 个字符:

grouped = df.groupby(df.quarter.str[:-2])
for name, group in grouped:
print(name)
print(group, 'n')

输出:

1947
quarter  some_value
0  1947q1           1
1  1947q2           3
2  1947q3           2
3  1947q4           4 
1948
quarter  some_value
4  1948q1           5 

附加评论:我使用了一个始终可以应用于字符串的操作。例如,检查以下内容:

s = 'Hi there, Dhruv!'
#Prints the first 2 characters of the string
print(s[:2])
#Output: "Hi"

#Prints everything after the third character
print(s[3:])
#Output: "there, Dhruv!"
#Prints the text between the 10th and the 15th character
print(s[10:15])
#Output: "Dhruv"

最新更新