My Coding > Programming language > Python > Python libraries and packages > Python Panda

Python Panda (Page: 4)

Go to Page:

  1. Panda Series;
  2. Pandas DataFrame: Creation;
  3. Pandas: Create test DataFrame;
  4. Pandas DataFrame: Add/Remove;
  5. Pandas DataFrame: Export/Import;
  6. Panda search and select;
  7. Pandas Cheat Sheet;
  8. Pandas: MultiIndex DataFrame;

Adding/Removing data to the DataFrame

it is possible to add column with identical value, or add a column with individual values

Add column with identical value

Add a column length and fill it with uniform value


restored_df_number_dict['length'] = 1
print(restored_df_number_dict)
#  european roman  length
#0     zero     0       1
#1      one     I       1
#2      two    II       1
#3    three   III       1
#4     four    IV       1
#5     five     V       1

Add column with individual values

To add individual values, you need to supply then as a list, and you need to make sure that the size of this list is matching the size of the DataFrame


restored_df_number_dict['length'] = [1, 1, 2, 3, 2, 1]
print(restored_df_number_dict)
#  european roman  length
#0     zero     0       1
#1      one     I       1
#2      two    II       2
#3    three   III       3
#4     four    IV       2
#5     five     V       1

Delete column from panda DataFrame

It is possible to remove full column with destroying all te data in this column with command del


del restored_df_number_dict['length'] 
print(restored_df_number_dict)
#  european roman
#0     zero     0
#1      one     I
#2      two    II
#3    three   III
#4     four    IV
#5     five     V

Adding row with selected index

It is possible to add row with any index, even negative. If this index already exists - it will be overwritten


restored_df_number_dict.loc[10] = ['six', 'VI']
# if you will use existing index - you will rewrite the data
# index can be negative as well
#   european roman
#0      zero     0
#1       one     I
#2       two    II
#3     three   III
#4      four    IV
#5      five     V
#10      six    VI

Deleting row with selected index

Removing row with selected index is a bit more complicated


restored_df_number_dict = restored_df_number_dict.drop(restored_df_number_dict.index[0])
#   european roman
#1       one     I
#2       two    II
#3     three   III
#4      four    IV
#5      five     V
#10      six    VI

Go to Page: 1; 2; 3; 4; 5; 6; 7; 8;


Published: 2021-11-05 09:11:16
Updated: 2021-12-17 02:48:39

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.