Programming Articles - Page 1105 of 3363

C program to remove a line from the file

Bhanu Priya
Updated on 31-Aug-2021 12:51:33

10K+ Views

A file is a physical storage location on disk and a directory is a logical path which is used to organise the files. A file exists within a directory.The three operations that we can perform on file are as follows −Open a file.Process file (read, write, modify).Save and close file.AlgorithmAn algorithm is given below to explain the C program to remove a line from the file.Step 1 − Read file path and line number to remove at runtime.Step 2 − Open file in read mode and store in source file.Step 3 − Create and open a temporary file in write ... Read More

C program to store even, odd and prime numbers into separate files

Bhanu Priya
Updated on 31-Aug-2021 12:49:33

4K+ Views

A file is a physical storage location on disk and a directory is a logical path which is used to organise the files. A file exists within a directory.The three operations that we can perform on file are as follows −Open a file.Process file (read, write, modify).Save and close file.ProgramFollowing is the C program to store even, odd and prime numbers into separate files − Live Demo#include #include /* Function declarations */ int even(const int num); int prime(const int num); int main(){    FILE * fptrinput,    * fptreven,    * fptrodd,    * fptrprime;    int num, success; ... Read More

C program to change the file name using rename() function

Bhanu Priya
Updated on 01-Sep-2021 09:08:30

3K+ Views

The rename function changes a file or directory from oldname to newname. This operation is just like a move operation. Hence, we can also use this rename function to move a file.This function is present in stdio.h library header files.The syntax of rename function is as follows −int rename(const char * oldname, const char * newname);Function of rename()It accepts two parameters. One is oldname and another is newname.These two parameters are pointer to constant character, which define old and new name of file.If file renamed is successful, then it returns zero otherwise, it returns a non-zero integer.During the rename operation ... Read More

Combining two Series into a DataFrame in Pandas

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:23:44

2K+ Views

To combine two series into a DataFrame in Pandas, we can take two series and concatenate them using concat() method.StepsCreate series 1 with two elements, where index is ['a', 'b'] and name is Series 1.Print Series 1.Make Series 2 with two elements, where index is ['a', 'b'] and name is Series 2.Print Series 2.Concatenate Pandas objects along a particular axis with optional set logic along the other axes.Print the resultant DataFrame.Example Live Demoimport pandas as pd s1 = pd.Series([4, 16], index=['a', 'b'], name='Series 1') print "Input series 1 is: ", s1 s2 = pd.Series([3, 9], index=['a', 'b'], name='Series 2') print "Input series 2 is: ... Read More

How to sort multiple columns of a Pandas DataFrame?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:21:38

988 Views

To sort multiple columns of a Pandas DataFrame, we can use the sort_values() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a variable col to sort the column.Print the sorted DataFrame.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 7, 0],       "y": [4, 7, 5, 1],       "z": [9, 3, 5, 1]    } ) print "Input DataFrame is:", df col = ["x", "y"] df = df.sort_values(col, ascending=[False, True]) print "After sorting column ", col, "DataFrame is:", dfOutputInput DataFrame is:    x  y  z 0  5  4  9 1  2  7  3 2  7  5  5 3  0  1  1 After sorting column ['x', 'y'] DataFrame is:    x  y  z 2  7  5  5 0  5  4  9 1  2  7  3 3  0  1  1

How to sort a column of a Pandas DataFrame?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:20:01

457 Views

To sort a column in a Pandas DataFrame, we can use the sort_values() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print input DataFrame, df.Initialize a variable col to sort the column.Print the sorted DataFrame.Example Live Demoimport pandas as pd df = pd.DataFrame(    {      "x": [5, 2, 7, 0],      "y": [4, 10, 5, 1],     `"z": [9, 3, 5, 1]   } ) print "Input DataFrame is:", df col = "x" df = df[col].sort_values(ascending=False) print "After sorting column ", col, "DataFrame is:", dfOutputInput DataFrame is:    x  y  z 0  5  4  9 1  2 10  3 2  7  5  5 3  0  1  1 After sorting column x DataFrame is: 2  7 0  5 1  2 3  0 Name: x, dtype: int64

How to use the apply() function for a single column in Pandas?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:15:24

16K+ Views

We can use apply() function on a column of a DataFrame with lambda expression.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print input DataFrame, df.Override column x with lambda x: x*2 expression using apply() method.Print the modified DataFrame.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 1, 5],       "y": [4, 10, 5, 10],       "z": [1, 1, 5, 1]    } ) print "Input DataFrame is:", df df['x'] = df['x'].apply(lambda x: x * 2) print "After applying multiplication of 2 DataFrame is:", dfOutputInput DataFrame is:    x  y  z 0  5  4  1 1  2 10  1 2  1  5  5 3  5 10  1 After applying multiplication of 2 DataFrame is:     x  y   z 0  10  4   1 1   4 10   1 2   2  5   5 3  10 10   1

Count the frequency of a value in a DataFrame column in Pandas

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:13:18

4K+ Views

To count the frequency of a value in a DataFrame column in Pandas, we can use df.groupby(column name).size() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Print frequency of column, x.Print frequency of column, y.Print frequency of column, z.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 1, 5],       "y": [4, 10, 5, 10],       "z": [1, 1, 5, 1]    } ) print "Input DataFrame is:", df col = "x" count = df.groupby('x').size() print "Frequency of values in column ", col, ... Read More

How to check if a column exists in Pandas?

Rishikesh Kumar Rishi
Updated on 30-Aug-2021 12:11:12

9K+ Views

To check if a column exists in a Pandas DataFrame, we can take the following Steps −StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a col variable with column name.Create a user-defined function check() to check if a column exists in the DataFrame.Call check() method with valid column name.Call check() method with invalid column name.Example Live Demoimport pandas as pd def check(col):    if col in df:       print "Column", col, "exists in the DataFrame."    else:       print "Column", col, "does not exist in the DataFrame." df = pd.DataFrame( ... Read More

How to select all columns except one in a Pandas DataFrame?

Rishikesh Kumar Rishi
Updated on 13-Sep-2023 15:54:47

35K+ Views

To select all columns except one column in Pandas DataFrame, we can use df.loc[:, df.columns != ].StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a variable col with column name that you want to exclude.Use df.loc[:, df.columns != col] to create another DataFrame excluding a particular column.Print the DataFrame without col column.Example Live Demoimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 1, 9],       "y": [4, 1, 5, 10],       "z": [4, 1, 5, 0]    } ) print("Input DataFrame is:", df) col ... Read More

Advertisements