数值框架python的总值

时间:2018-04-12 14:10:46

标签: python pandas dataframe

我正在尝试获取这些表的总值

building_weight_and_height = {'Floor': ['Roof','10th','9th','8th','7th','6th','5th','4th','3rd','2nd'],
                              'DL(kN)': [1200,1200,1200,1200,1200,1200,1200,1200,1200,1200],
                              'StoreyHeight': [3,3,3,3,3,3,3,3,3,4]}

预期产出:

Output image

3 个答案:

答案 0 :(得分:1)

快速的方法,我相信还有其他方法:

import pandas as pd

dics = {'Floor': ['Roof','10th','9th','8th','7th','6th','5th','4th','3rd','2nd'],
     'DL(kN)': [1200,1200,1200,1200,1200,1200,1200,1200,1200,1200],
     'StoreyHeight': [3,3,3,3,3,3,3,3,3,4]}

df = pd.DataFrame(columns=['Floor','DL(kN)','StoreyHeight'])
print (df)

df['Floor'] = dics['Floor']
df['DL(kN)'] = dics['DL(kN)']
df['StoreyHeight'] = dics['StoreyHeight']
totalDL = df['DL(kN)'].sum()
totalSH = df['StoreyHeight'].sum()
df=df.append({'Floor':'Total','DL(kN)':totalDL,'StoreyHeight':totalSH},ignore_index=True)
print (df)

输出如下:

    Floor  DL(kN)  StoreyHeight
0    Roof    1200             3
1    10th    1200             3
2     9th    1200             3
3     8th    1200             3
4     7th    1200             3
5     6th    1200             3
6     5th    1200             3
7     4th    1200             3
8     3rd    1200             3
9     2nd    1200             4
10  Total   12000            31

答案 1 :(得分:0)

你有什么问题?

    <LinearLayout
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:orientation="vertical"
          >
          <RelativeLayout
              android:id="@+id/REFReLayTellFriend"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              >
          <EditText
              android:id="@+id/etpass1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="@android:color/transparent"
              android:bottomLeftRadius="10dp"
              android:bottomRightRadius="50dp"
              android:fontFamily="@font/frutiger"
              android:gravity="start"
              android:inputType="textPassword"
              android:hint="@string/regpass_pass1"
              android:padding="20dp"
              android:paddingBottom="10dp"
              android:textColor="#000000"
              android:textColorHint="#d3d3d3"
              android:textSize="14sp"
              android:topLeftRadius="10dp"
              android:topRightRadius="10dp"/>
              <ImageButton
                  android:id="@+id/imgshowhide1"
                  android:layout_width="40dp"
                  android:layout_height="20dp"
                  android:layout_marginTop="20dp"
                  android:layout_marginRight="10dp"
                  android:background="@drawable/showpass"
                  android:layout_alignRight="@+id/etpass1"/>
          </RelativeLayout>    

 boolean show=true;
 //on image click inside password do this
 if(show){
                imgshowhide2.setBackgroundResource(0);
                imgshowhide2.setBackgroundResource(R.drawable.hide);
                etpass2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                etpass2.setSelection(etpass2.getText().length());

                show=false;
            }else{
                imgshowhide2.setBackgroundResource(0);
                imgshowhide2.setBackgroundResource(R.drawable.showpass);
                //etpass1.setInputType(InputType.TYPE_TEXT);
                etpass2.setInputType(InputType.TYPE_CLASS_TEXT |
                        InputType.TYPE_TEXT_VARIATION_PASSWORD);
                etpass2.setSelection(etpass2.getText().length());
                show=true;
            }

答案 2 :(得分:0)

从字典创建数据框,并使用pd.DataFrame.loc添加一行:

import pandas as pd

building_weight_and_height = {'Floor': ['Roof','10th','9th','8th','7th','6th','5th','4th','3rd','2nd'],
                              'DL(kN)': [1200,1200,1200,1200,1200,1200,1200,1200,1200,1200],
                              'StoreyHeight': [3,3,3,3,3,3,3,3,3,4]}

df = pd.DataFrame(building_weight_and_height)
df = df[['Floor', 'DL(kN)', 'StoreyHeight']]

# alternatively:
# df = pd.DataFrame(building_weight_and_height, columns=['Floor', 'DL(kN)', 'StoreyHeight'])

df.loc[len(df.index)+1] = ['Total', df['DL(kN)'].sum(), df['StoreyHeight'].sum()]

print(df)

#     Floor  DL(kN)  StoreyHeight
# 0    Roof    1200             3
# 1    10th    1200             3
# 2     9th    1200             3
# 3     8th    1200             3
# 4     7th    1200             3
# 5     6th    1200             3
# 6     5th    1200             3
# 7     4th    1200             3
# 8     3rd    1200             3
# 9     2nd    1200             4
# 11  Total   12000            31