我有一个数据框(df
):
0 1 2 3 4 5
0 6894 JONES STEVE D 2017-01-03 00:00:00 None
1 13555 SMITH JENNY E 2017-04-01 00:00:00 None
我有一个名为new_header
的标头,该标头将应用于数据帧,如下所示:
0 Identifier
1 Surname
2 First name(s)
3 Transferred to
4 Transfer Date
5 Status
我将new_header
应用于以下内容:
df.columns = new_header
我得到以下输出:
0 Identifier Surname ... Transfer Date Status
0 6894 JONES ... 2017-01-03 00:00:00 None
1 13555 SMITH ... 2017-04-01 00:00:00 None
在df
的左上角,您可以看到0
。有人可以让我知道如何删除0,以便我的数据框看起来像:
Identifier Surname ... Transfer Date Status
0 6494 JONES ... 2017-01-03 00:00:00 None
1 13595 SMITH ... 2017-04-01 00:00:00 None
答案 0 :(得分:2)
您正在尝试使用pandas.Series
对象重命名列。这样做时,Series
的名称将转换为结果pandas.Index
对象的名称。您可以通过多种方式解决此问题。
df.columns
分配一个没有@anky_91建议的名称的df.columns = new_header.values
df
Identifier Surname First name(s) Transferred to Transfer Date Status
0 6894 JONES STEVE D 2017-01-03 00:00:00 None
1 13555 SMITH JENNY E 2017-04-01 00:00:00 None
df.columns
的系列分配给None
df.columns = new_header.rename(None)
df
Identifier Surname First name(s) Transferred to Transfer Date Status
0 6894 JONES STEVE D 2017-01-03 00:00:00 None
1 13555 SMITH JENNY E 2017-04-01 00:00:00 None
df.columns = new_header
df.rename_axis(None, 1)
Identifier Surname First name(s) Transferred to Transfer Date Status
0 6894 JONES STEVE D 2017-01-03 00:00:00 None
1 13555 SMITH JENNY E 2017-04-01 00:00:00 None
rename
与new_header
一起用作重命名映射这假设new_header
的索引与df
中被重命名的索引对齐。
另外,我必须变得棘手,并确保它们是同一类型。
df.rename(columns=int).rename(columns=new_header)
Identifier Surname First name(s) Transferred to Transfer Date Status
0 6894 JONES STEVE D 2017-01-03 00:00:00 None
1 13555 SMITH JENNY E 2017-04-01 00:00:00 None
答案 1 :(得分:0)
您可以按名称获取或设置索引
public class UploadImage_Fragment extends Fragment implements View.OnClickListener {
public static final String TAG="###Upload Image###";
TextView uploadText;
ImageView uploadImage;
Button uploadButton;
App_Functions functions;
String cmpImage,Base64Image;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.upload_profile_cover_registration,container,false);
uploadText = v.findViewById(R.id.textViewImageUpload);
uploadImage = v.findViewById(R.id.uploadImage);
uploadButton = v.findViewById(R.id.uploadButton);
uploadButton.setEnabled(false);
Glide.with(getActivity()).load(R.drawable.cam_icon).into(uploadImage);
uploadImage.setOnClickListener(this);
uploadButton.setOnClickListener(this);
setHasOptionsMenu(true);
return v;
}
@Override
public void onClick(View view) {
int clickID=view.getId();
switch (clickID){
case R.id.uploadImage:
Media_Gallery gallery = new Media_Gallery();
gallery.setTargetFragment(this,101);
FragmentTransaction fragmentTrasaction=getFragmentManager().beginTransaction();
fragmentTrasaction.addToBackStack("Chat");
fragmentTrasaction.replace(R.id.FragmentLoginRegistration,gallery,"Chat");
fragmentTrasaction.commit();
break;
case R.id.uploadButton:
if (uploadButton.isEnabled()){
Log.d(TAG,"Button Is Enable Upload Image Here");
uploadButton.setEnabled(false);
}else {
Toast.makeText(getActivity(), "Select Image To Upload", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
functions=new App_Functions(getActivity());
if (requestCode==301 && resultCode== Activity.RESULT_OK){
Uri uri = data.getData();
cmpImage = functions.compressImage(uri,true);
Glide.with(getActivity()).load(cmpImage).apply(RequestOptions.circleCropTransform()).into(uploadImage);
uploadButton.setEnabled(true);
Log.d(TAG,"Image "+uri+" Compress Image "+cmpImage);
Bitmap bitmap = BitmapFactory.decodeFile(cmpImage);
Base64Image=functions.Convert_To_Base64(bitmap);
}else if (requestCode == 101 && resultCode == Activity.RESULT_OK){
ArrayList<String> imagePath = data.getStringArrayListExtra("Profile");
if (imagePath!=null){
String image = imagePath.get(0);
Log.d(TAG,"Image Path "+image);
uploadText.setText("Hey I am New Test"); // Not working
uploadImage.setImageDrawable(null);
uploadImage.invalidate();
File file = new File(image);
Uri newPath = Uri.fromFile(file);
Log.d(TAG,"Check File "+file);
Log.d(TAG,"Check Uri "+newPath);
Log.d(TAG,"Check ImageView "+uploadImage);
Glide.with(getActivity()).load(image).into(uploadImage); // Not working
}
}
}
@Override
public void onResume() {
super.onResume();
uploadImage.setImageDrawable(null);
uploadImage.invalidate();
}
}
请参见docs