简体   繁体   中英

How to convert dataframe to numpy?

I have the following pd.DataFrame named df and I am checking for this condition: df = df[df['Name'].str.len()>5] . This condition will remove 4th and 5th row from df .

 Id                           Name   
  1          lib_mysqludf_sys.html   
  2             lib_mysqludf_sys.c   
  3            lib_mysqludf_sys.so  
  4                           bash   
  5                            lib

How can I convert this pd.DataFrame to numpy and apply that condition to it.

You have to call the to_numpy() method on your selection. See the following example:

import pandas as pd

df = pd.DataFrame({"Name": 
        ["lib_mysqludf_sys.html",
         "lib_mysqludf_sys.c",
         "lib_mysqludf_sys.so",
         "bash",
         "lib"]})

cnd = df["Name"].str.len() > 5
res = df[cnd].to_numpy()

res is a two-dimensional numpy array:

# print(res)
array([['lib_mysqludf_sys.html'],
       ['lib_mysqludf_sys.c'],
       ['lib_mysqludf_sys.so']], dtype=object)

If you need only a one-dimensional array, you can get that with res.flatten() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM