Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract specific columns without index no. and with all the rows in python dataframe?

#df

index  a   b   c
1      2   3   4
2      3   4   5

Please help me how to extract columns "a" and "c" with all the rows but without the index column.

df[["a","c"]] # But index no. is also coming, so how to remove the index no.?

like image 434
Goutam Avatar asked Apr 17 '20 13:04

Goutam


People also ask

How do I get a column of a data frame without an index?

You can use DataFrame. to_string(index=False) on the DataFrame object to print. To result DataFrame. to_string() function is a string of the DataFrame without indices.

How do I get specific columns in pandas?

To select a single column, use square brackets [] with the column name of the column of interest.

How do you select all columns of a DataFrame except the ones specified?

To select all columns except one column in Pandas DataFrame, we can use df. loc[:, df. columns != <column name>].


Video Answer


1 Answers

DataFrames and Series will always have an index, you can use:

df[["a","c"]].values

output:

array([[2, 4],
       [3, 5]], dtype=int64)
like image 104
kederrac Avatar answered Oct 12 '22 18:10

kederrac