Python 合并在不同列中拆分的值,覆盖0值
我有一个dataframe,其中一些值被拆分为不同的列 然后我想合并所有这些,所以结果应该是 我查看了熊猫教程,但找不到类似的内容 可能不太难,但我正在运行时间您可以使用Python 合并在不同列中拆分的值,覆盖0值,python,pandas,merge,overwrite,Python,Pandas,Merge,Overwrite,我有一个dataframe,其中一些值被拆分为不同的列 然后我想合并所有这些,所以结果应该是 我查看了熊猫教程,但找不到类似的内容 可能不太难,但我正在运行时间您可以使用.max()并通过指定axis=1来按行应用它 import pandas as pd # your data # =========================== c1 = [0,0,0,0,2,7] c2 = [0,0,8,4,0,0] c3 = [5,3,0,0,0,0] df = pd.DataFrame(d
.max()
并通过指定axis=1来按行应用它
import pandas as pd
# your data
# ===========================
c1 = [0,0,0,0,2,7]
c2 = [0,0,8,4,0,0]
c3 = [5,3,0,0,0,0]
df = pd.DataFrame(dict(C1=c1,C2=c2,C3=c3))
print(df)
C1 C2 C3
0 0 0 5
1 0 0 3
2 0 8 0
3 0 4 0
4 2 0 0
5 7 0 0
# processing
# ===========================
df.max(axis=1)
0 5
1 3
2 8
3 4
4 2
5 7
dtype: int64
您可以使用.max()
并通过指定axis=1
import pandas as pd
# your data
# ===========================
c1 = [0,0,0,0,2,7]
c2 = [0,0,8,4,0,0]
c3 = [5,3,0,0,0,0]
df = pd.DataFrame(dict(C1=c1,C2=c2,C3=c3))
print(df)
C1 C2 C3
0 0 0 5
1 0 0 3
2 0 8 0
3 0 4 0
4 2 0 0
5 7 0 0
# processing
# ===========================
df.max(axis=1)
0 5
1 3
2 8
3 4
4 2
5 7
dtype: int64
您只需调用并按行求和即可:
In [134]:
df.sum(axis=1)
Out[134]:
0 5
1 3
2 8
3 4
4 2
5 7
dtype: int64
使用sum
按行处理任意数量的0
值您只需调用并按行求和即可:
In [134]:
df.sum(axis=1)
Out[134]:
0 5
1 3
2 8
3 4
4 2
5 7
dtype: int64
使用sum
按行处理任意数量的0
值不清楚C是其他列的和还是最大值。每行中是否只有一个非零列?不清楚C是其他列的和还是最大值。每行中是否只有一个非零列?很好,但如果我只想求和或从特定列中取最大值,该怎么办?更准确地说,尽管这些值被拆分,但其中一些具有相同的名称(重复)。然后我想合并列repeatedGreat,但是如果我只想对特定列求和或取最大值呢?更准确地说,尽管这些值被拆分,但其中一些具有相同的名称(重复)。然后我要合并重复的列