I have the following data frame, I want to iterate all the rows dynamically, to find bytes and convert them to float
Example
col1 col2 col3
None None b'R\xb8\x1e%\xda\x16QA'
b'R\xb8\x1e%\xda\x16QA' b'R\xb8\x1e%\xda\x16QA' None
None None None
b'R\xb8\x1e%\xda\x16QA' None None
b'R\xb8\x1e%\xda\x16QA' None b'R\xb8\x1e%\xda\x16QA'
Mi function bytes to float
def bytes2float(byte):
if byte:
# do stuff
I want to iterate over all the rows, because the DataFrame is dynamic at the moment I can only do it statically, because I know how many columns the DataFrame has.
Something like that
for index, row in mydf.iterrows():
# print(row['col1'], row['col2'])
bytes2float(row['col1'])
bytes2float(row['col2'])
bytes2float(row['col3'])
...
...
Any ideas or suggestions?
You can iterate over mydf.columns
for index, row in mydf.iterrows():
for col in mydf.columns:
bytes2float(row[col])
If you want to run the method bytes2float
on every column of every row then you can use applymap
Sample:
import pandas
import numpy as np
df = pd.DataFrame(np.arange(6).reshape(2,3))
print (df.applymap(lambda x: f"*{x}*"))
Output:
0 1 2
0 *0* *1* *2*
1 *3* *4* *5*
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.